I use WordPress and list data from my API:
$url = "http://example.com/all-list";
$response = wp_remote_get( $url );
$data_body = json_decode( wp_remote_retrieve_body( $response ), true );
<?php foreach ( $data_body as $real_data ): ?>
<h1><?php echo $real_data["title"]; ?></h1>
<?php end foreach; ?>
But in I don't know how to give url.
Example: I'd like to give <a href="/real-data?id=<?php $real_data['id'] ?>"> then return new view template. It like get_permalink(); to view single page post.
How could I do this in WordPress?
Check this examples below.
// This would output '/client/?s=word&foo=bar'
echo esc_url( add_query_arg( 'foo', 'bar' ) );
// This would output '/client/?s=word&foo=bar&baz=tiny'
$arr_params = array( 'foo' => 'bar', 'baz' => 'tiny' );
echo esc_url( add_query_arg( $arr_params ) );
https://developer.wordpress.org/reference/functions/add_query_arg/
Related
I'm trying to create a list of posts that has a seperator displaying each month.
It's supposed to look something like this:
January 2018
- blog post
- blog post
Febuary 2018
- blog post
etc....
The code i'm using is a simple loop. I've tried to get the seperator working using the the_date() function and change its date format. But it still triggers for each day.
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1
);
$the_query = new WP_Query($args); if($the_query->have_posts()): while($the_query->have_posts()): $the_query->the_post();
?>
<?php if(!the_date('F Y','','',false) == ''){ ?>
<div class="seperator">
<?php the_time('F Y'); ?> //echo month and year
</div>
<?php } ?>
<div class="post"> //the post content </div>
<?php endwhile; endif; wp_reset_query(); ?>
I don't really know what code to use to make it only trigger each month.
You should try something like this:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC'
);
$the_query = new WP_Query($args); if($the_query->have_posts()): while($the_query->have_posts()): $the_query->the_post();
if ($lastMonth && the_date('mm') !== $lastMonth) {
// new month
}
$lastMonth = the_date('mm');
?>
This way, in each iteration you're setting the month of publication in the lastMonth variable. The next iteration, you're checking if the month of publication is equal to the previous one. If not, you're in a new loop and you can insert your seperator.
I'm looking for a succinct method of making get_next_post()
double back to the beginning once it hits the last post.
Currently, it stops once it hits the final post.
Here are a few lines of code from the codex
for context that are similar to what I'm using:
<?php $next_post = get_next_post();
if (!empty( $next_post )): ?>
<a href="<?php echo get_permalink( $next_post->ID ); ?>">
<?php echo $next_post->post_title; ?>
</a>
<?php endif; ?>
http://codex.wordpress.org/Function_Reference/get_next_post
Thanks in advance for your suggestion.
you can't do it with get_next_post - full stop.
Here's how I've done it...
<?php
/**
* Infinite next and previous post looping in WordPress
*/
$next_post = get_next_post();
$previous_post = get_previous_post();
$current_id = get_the_ID();
// Get ID's of posts
$next_id = $next_post->ID;
$previous_id = $previous_post->ID;
$prev_title = strip_tags(str_replace('"', '', $previous_post->post_title));
$next_title = strip_tags(str_replace('"', '', $next_post->post_title));
// get the first posts ID etc
$args = array('post_type'=>'project', 'posts_per_page' => -1);
$posts = get_posts($args);
$first_id = $posts[0]->ID;
$first_title = get_the_title( $first_id );
// get the last posts ID etc
$last_post = end($posts);
$last_id = $last_post->ID; // To get ID of last post in custom post type outside of loop
$last_title = get_the_title( $last_id );
// if the current post isn't the first post
if($current_id != $first_id) { ?>
Previous Project:<?php echo $prev_title; ?>
<?php
// if the current post is the first post
} else { ?>
Previous Project
<?php }; ?>
<?php
// if the current post isn't the last post
if($current_id != $last_id) { ?>
<a rel="next" href="<?php echo get_permalink($next_id) ?>" title="<?php $next_title ?>" class="prev-next next"> Next Project <?php echo $next_title; ?></a>
<?php
// if the current post is the last post
} else { ?>
<a rel="next" href="<?php echo get_permalink($first_id) ?>" title="<?php $last_title ?>" class="prev-next next"> Next Project <?php echo $first_title; ?></a>
<?php } ?>
Elements take from here Getting first & last post in custom post type outside of loop
I've got a single page website with a front-page.php that shall include all my custom templates (each one is a static page).
<?php get_header();
$args = array(
'sort_order' => 'ASC',
'sort_column' => 'menu_order',
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages($args);
foreach ($pages as $page_data) {
$content = apply_filters('the_content', $page_data->post_content);
?>
<?php echo "$content" ?>
<?php
}
get_footer();
?>
With "the_content" it references only the content of my text in the backend, but not all my tags surrounding my the_content tag in the template itself, for example:
<?
/*
Template Name: Features
*/
the_post()
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<section>
<? the_content() ?>
</section>
<?php endif; ?>
In this case, the surrounding tags section aren't selected and don't appear in the markup.
What do I have to change in my front-page.php to select the whole template, not only its template?
Thanks so much in advance!
You could put the loop part of each page template in a separate template part and call the parts dynamically on your front page with get_template_part:
foreach ( $pages as $page_data ) {
$template = get_post_meta( $page_data->ID, '_wp_page_template', true ); // get page template path
$template = substr( $template, 0, -4 ); // chop off .php from the string
get_template_part( 'loop', $template );
}
For a template 'Features' (filename features.php) it would look for loop-features.php, etc. Fallback will be loop.php.
I have this code to display the attached images on a post:
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<a href="';
echo wp_get_attachment_url( $attachment->ID );
echo '" rel="lightbox">';
echo wp_get_attachment_image( $attachment->ID, 'large' );
echo '</a>';
}
}
?>
But i cannot work out why the Lightbox is not working! I have tried colorbox, shadowbox, ligthbox plugins etc... it just does not load. When you click on the image, it just opens in page.
What am i doing wrong?
See if your theme call
wp_head();
in your header file and
wp_footer();
in your footer file the plugins don't work if youar missing this.
I have post about this before, but I didn't get any conclusive answer but I'm really hoping someone can help me. I have setup some custom post types, and with them, some custom fields using Wordpress 3's UI.
One of the fields I have set up is called banner_image, but in the loop, it doesn't output the image.
<?php echo get_post_meta($post->ID, 'banner_image', true); ?>
This simply outputs the ID number of the post. If I set the function to false, I get an array with this ID in and nothing else. How do I get the path to the image? I can't work this out and Googling reveals a sea of content not related to my problem, it's a real difficult one to search for so you're my only hope!
Many thanks,
Michael.
<?php
global $post;
$tmp_post = $post;
$args = array(
'post_status' => 'publish',
'post_type' => 'work',
'order' => 'DESC'
);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<?php if( get_post_meta($post->ID, 'show_in_home_banner', true) == "yes" ) { ?>
<li class="slide">
<div class="slide-image">
<a href="<?php echo get_page_link($post->ID) ?>">
<?php echo get_post_meta($post->ID, 'banner_image', true); ?>
</a>
</div>
<div class="slide-content">
<h3 class="slide-header"><?php echo get_post_meta($post->ID, 'sub_title', true); ?></h3>
<p class="slide-title"><strong><?php echo the_title(); ?></strong></p>
</div>
</li>
<?php } ?>
<?php endforeach; ?>
Try this
<?php echo get_post_meta($post->ID, 'banner_image', $single); ?>
Apparently, the custom field 'banner_image' doesn't have the right value. I guess it doesn't save the correct value first. You may want to install the Simple WP FirePHP plugin (http://wordpress.org/extend/plugins/simple-wp-firephp/) and check the value with the fb() function.