Wordpress post->ID Problem - php

This is a wordpress question. I am trying to use a bit of code that works just fine on my home page on my inner page templates:
query_posts('cat=4');
// The Loop
echo '<div id="cal_details"><ul>';
while ( have_posts() ) : the_post();
$cal_date_j = date('j', intval(get_post_meta($post->ID, 'date_value', true)));
$cal_date_n = date('n', intval(get_post_meta($post->ID, 'date_value', true)));
$my_array[] = date('j, n', intval(get_post_meta($post->ID, 'date_value', true)));
$issetdate = get_post_meta($post->ID, 'date_value', true);
if (isset($issetdate)) {
echo '<li class="cal_event_li list_item_' . $cal_date_j . '_' . $cal_date_n . '">';
echo '<a href="' . get_permalink() . '">';
the_title();
echo '</a></li>';
}
endwhile;
echo '</ul></div>';
However, this doesn't seem to work on the inner-pages. All the titles links are being outputted correctly but it won't print the get_post_meta part correctly.
The list items all display something like <li class="cal_event_li list_item_1_1">
I think there is perhaps some issue with the way I have tried to use $post->ID but Im not sure whats going on here. Any ideas?

When you use query_posts you have to call global $post to get the post_meta. If you're only calling one category why don't you just use an archive template?
Also if you're going to use query_posts make sure you reset the query afterwords so plugins, sidebars, etc can still interact with the loop for conditionals etc..
global %post;
query_posts('cat=4');
// The Loop
//more stuff
endwhile;
wp_reset_query();

try replacing $post->ID with the_ID() in the inner pages. something like this
query_posts('cat=4');
// The Loop
echo '<div id="cal_details"><ul>';
while ( have_posts() ) : the_post();
$cal_date_j = date('j', intval(get_post_meta(the_ID(), 'date_value', true)));
$cal_date_n = date('n', intval(get_post_meta(the_ID(), 'date_value', true)));
$my_array[] = date('j, n', intval(get_post_meta(the_ID(), 'date_value', true)));
$issetdate = get_post_meta(the_id(), 'date_value', true);
if (isset($issetdate)) {
echo '<li class="cal_event_li list_item_' . $cal_date_j . '_' . $cal_date_n . '">';
echo '<a href="' . get_permalink() . '">';
the_title();
echo '</a></li>';
}
endwhile;
echo '</ul></div>';

Related

WordPress Infinite Loop on Single Post

I've taken some code from https://gist.github.com/banago/5603826 so that I am able to do previous/next posts for a specific post on my WordPress site which is a custom post type of 'product'.
<?php
if( get_adjacent_post(false, '', true) ) {
previous_post_link('%link', '← Previous Post');
} else {
$first = new WP_Query('post_type=products&posts_per_page=1&order=DESC'); $first->the_post();
echo '← Previous Post';
wp_reset_query();
};
if( get_adjacent_post(false, '', false) ) {
next_post_link('%link', 'Next Post →');
} else {
$last = new WP_Query('post_type=products&posts_per_page=1&order=ASC'); $last->the_post();
echo 'Next Post →';
wp_reset_query();
};
?>
However it doesn't actually infinitely loop, if it's on the first or last post, the link outputs the page it's already on. An example can be found here - http://s860623395.websitehome.co.uk/products/bespoke-build/
Any solutions to this? Thanks!
Was just working on this myself the other day for a portfolio.
Here is what worked for me (adjusted for your product loop)
It should show a single next product indefinitely (infinite loop)
if( get_adjacent_post(false, '', true) ) {
$previous_link_url = get_permalink( get_previous_post() );
echo '← Next Product';
} else {
$first = new WP_Query('post_type=product&posts_per_page=1&order=DESC');
$first->the_post();
echo 'Next Product →';
wp_reset_query();
};
Just a note, next post is actually the previous post, It's a confusing thing, but when you view the latest post, be it portfolio, product or whatever, the next one is actually the one before, otherwise the next post after the most recent published post would then be the oldest one. This seems to be how most have done it within an infinite loop.
Like you, I tried many variations, none of which really worked, but this one did. Currently I'm using it in the Genesis framework for a portfolio CPT with a background image.
Here is the full working code below taken from a child theme:
add_action( 'genesis_after_content_sidebar_wrap', 'go_prev_next_post_nav_cpt', 999 );
/**
* Enable next link in portfolio.
*
* #since 1.0.0
*/
function go_prev_next_post_nav_cpt() {
echo '<div class="adjacent-entry-pagination">';
echo '<div class="wrap">';
if( get_adjacent_post(false, '', true) ) {
$prevThumb = get_the_post_thumbnail_url( get_previous_post(), 'full' );
$previous_link_url = get_permalink( get_previous_post() );
$prevTitle = get_the_title( get_previous_post() );
echo '<a class="post-link bg-image bg-overlay" href="' . $previous_link_url . '" style="background-image:url(' . $prevThumb . ')"><div class="next-description"><p class="mast">Next project</p><h3 class="display-2">' . $prevTitle . '</h3></div></a>';
} else {
$first = new WP_Query('post_type=portfolio&posts_per_page=1&order=DESC');
$first->the_post();
$bg_image = get_the_post_thumbnail_url();
echo '<a class="post-link bg-image bg-overlay" href="' . get_permalink() . '" style="background-image: url(' . $bg_image . ')"><div class="next-description"><p class="mast">Next project</p><h3 class="display-2">' . get_the_title() . '</h3></div></a>';
wp_reset_query();
};
echo '</div>';
echo '</div>';
}
From memory, I used this as a starting point (lot a trial and error!). https://gist.github.com/banago/5603826

How can i display only the first image from this JSON array?

OK, I'm trying to integrate an API that lists adoptable pets into a wordpress website. I've done lots of googling and read through tutorials, and so far have managed to put together a super basic plugin that seems to do what I'm trying to accomplish. Currently I'm trying to pull in an image, but just the first image. Each animal may have 5 images associated with it, but I only want to pull in the first (default). Currently my code brings them all. Now I realize the problem is that I'm using "foreach()". But, this is new to me and my googling is not going well, and any other way I've tried to do it is just not getting me ANY pictures. Any advice is appreciated....and if I'm doing anything else wrong, feel free to let me know :) I also need to figure out how to paginate it, but I'm thinking that's a separate question! Thanks!
<?php
add_shortcode('pets', 'petsshortcode');
function petsshortcode() {
$request = wp_remote_get( 'https://petstablished.com/api/v2/public/pets?public_key=UlEK4EWvDAoOjXeQXSCQZAyBywWfqfOg&search[status]=available,foster&pagination[limit]=20&pagination[page]=1' );
if( is_wp_error( $request ) ) {
return false; // Bail early
}
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );
if( ! empty( $data ) ) {
foreach( $data->collection as $collection ) {
echo '<div id="pet-block"><ul class="pet-profile"><li class="pet-name">'. $collection->name; echo '</li>';
echo '<li class="pet-meta">'.'<span class="sex">' . $collection->sex; echo '</span>' . '<span class="breed">' . $collection->breed; echo '</span><span class="age">' . $collection->age; echo '</span></li>';
echo '<li><p>' . $collection->description; echo '</li></p>';
foreach( $collection->images as $images ) {
echo '<div class="pet-photo"><img src="' . $images->image->url; echo '" width="200"></div>';}
echo '</ul></div>';
}
}
}
You don't need nested foreach loops in this case, simply use just one foreach loop like this:
// your code
foreach( $data->collection as $collection ) {
echo '<div id="pet-block"><ul class="pet-profile"><li class="pet-name">'. $collection->name; echo '</li>';
echo '<li class="pet-meta">'.'<span class="sex">' . $collection->sex; echo '</span>' . '<span class="breed">' . $collection->breed; echo '</span><span class="age">' . $collection->age; echo '</span></li>';
echo '<li><p>' . $collection->description; echo '</li></p>';
echo '<div class="pet-photo"><img src="' . $collection->images[0]->image->url; echo '" width="200"></div>';
echo '</ul></div>';
}
// your code

PHP if statement inside foreach to check WordPress pages

I have a foreach statement that loops through all of the pages of a WordPress installation and lists them inside a div (using the get_pages() function).
It currently looks like this:
<?php
$pages = get_pages();
foreach ( $pages as $page ) {
$linkPage = '<a class="order" href="' . get_page_link( $page->ID ) . '">';
$linkPage .= $page->post_title;
$linkPage .= '</a>';
echo $linkPage;
}
?>
What I need to do now is to add an if statement that inserts the string "current" after class="order... if the link is the one from the current page.
It may seem like a silly question, but I'm a front-end developer with little PHP experience, and every time I tried to add the if I got a syntax error, claiming there was an unexpected if statement.
I hope I made myself clear.
If any help can be provided, it will be very much appreciated.
<?php
//get id of your current page
$post_id = $post[0]->ID;
$pages = get_pages();
foreach ( $pages as $page ) {
$current = $post_id == $page->ID ? ' current' : '';
$linkPage = '<a class="order '.$current.'" href="' . get_page_link( $page->ID ) . '">';
$linkPage .= $page->post_title;
$linkPage .= '</a>';
echo $linkPage;
}
?>
Check this, I'm not sure if $post_id = $post[0]->ID works in your wp version, but the if-statement logic is correct and will work. Try to echo $post_id and $current if something wrong.
Try this Code :
<?php
//get id of your current page
$post_id = get_the_ID();
$pages = get_pages();
foreach ( $pages as $page ) {
//Condition statement to add the class current
$current = $post_id == $page->ID ? 'current' : '';
$linkPage = '<a class="order '.$current.'" href="' . get_page_link( $page->ID ) . '">';
$linkPage .= $page->post_title;
$linkPage .= '</a> <br> ';
echo $linkPage;
}
?>
Hope this will help you...

The_excerpt showing before the div

i've created latest post container, which i'm trying to create in a shortcode, however when i add the excerpt into the div it is not being shown as it would normally. it is being pushed before the div. The test string after the excerpt is being placed the correct place inside in the div. How come the excerpt is being pushed to the top?
if($insta->have_posts()) :
while($insta->have_posts()) :
$insta->the_post();
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
$post_excerpt = the_excerpt();
$permalink = get_permalink();
$output = '<div class="betting-item">'
. '<div class="betting-thumb">'
. '<a href=" '. $permalink .' ">'
. '<img class="thumbnail" src="'. $thumb_url[0] .'" alt="0" />" </a>'
. '</div>'
. '<div class="betting-description">'
. '<h6>Instalocket</h6>'
. '<div class="meta">November 18, 2014 • No Comments</div>'
. '<div>' . $post_excerpt[0] . ' test</div>'
. '</div>'
. '</div>';
endwhile;
endif;
That is happening because the_excerpt() will print the except there and then (so before the div), you need to use get_the_excerpt () which will allow you to print it out at a later stage (in the div).
EDIT:
This should work and only print the excerpt on the first loop:
if($insta->have_posts()) :
$count = 0;
while($insta->have_posts()) :
$insta->the_post();
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
if($count == 0)
{
$post_excerpt = the_excerpt();
$count = 1;
}
else
{
$post_excerpt = '';
}
$permalink = get_permalink();

How to add if statement inside loop

On a wordress site I'm building, for the first time, I'm trying to add custom loops.
<?php
$custom_loop = new WP_Query('showposts=5&category_name=Featured&orderby=rand');
if ( $custom_loop->have_posts() ) :
echo '<ul>';
while ( $custom_loop->have_posts() ):
$custom_loop->the_post();
echo '<li>' . get_the_title() . '
</li>';
endwhile;
wp_reset_query();
echo '</ul>';
endif;
?>
How do I correctly add this line if ( has_post_thumbnail() ) {
the_post_thumbnail();} inbetween the <li></li> tags?
I've tried just placing it in, but it shows up like normal text on the site.
If you want to put the_post_thumbnail() value between li tag, use below code instead of your echo '<li>.....';
echo '<li>' . get_the_title() . '';
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
echo '</li>';
Try this:
<?php
$custom_loop = new WP_Query('showposts=5&category_name=Featured&orderby=rand');
if ($custom_loop->have_posts()) {
echo '<ul>';
while ($custom_loop->have_posts()) {
$custom_loop->the_post();
$thumb = (has_post_thumbnail()) ? the_post_thumbnail() : '';
printf('<li>%s%s</li>', get_permalink(), get_the_title(), $thumb);
}
wp_reset_query();
echo '</ul>';
}
?>

Categories