Im just wondering how I could get the post id of the blog post I am currently reading while in another loop (i.e. the recent posts loop in the sidebar)
In my single post php file I have this code creating a variable
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'single' ); ?>
<?php $current_post_id = get_the_ID(); ?>
<?php endwhile; // end of the loop. ?>
Then in my default-widgets php file, in the loop for recent posts I have this...
<?php while ($r->have_posts()) : $r->the_post(); ?>
<?php $recent_post_id = get_the_ID(); ?>
<li> <?php echo $recent_post_id; if ( $recent_post_id == $current_post_id ) { echo 'pass '; } else { echo 'fail ';} ?></li>
<?php endwhile; ?>
Its giving fail every time, so obviously what Im doing doesnt make sense (Im still learning). Im just wondering is there a way to grab the post id from the first loop, and use it in the second. At the moment when I echo current_post_id in the second loop nothing shows up.
(Regarding highlighting the current post, that'll be easy once I get this working.
Thanks for the help.
There are a lot of examples online on how to get the ID outside the loop.
try this in the widget code:
<?php
global $post;
$current_post_id = $post->ID;
while ($r->have_posts()) : $r->the_post(); ?>
<li> <?php echo $recent_post_id; if ( $recent_post_id == $current_post_id ) { echo 'pass '; } else { echo 'fail ';} ?><?php if ( get_the_title() ) the_title(); else the_ID(); ?></li>
<?php endwhile; ?>
Let us know if it works.
Related
In Wordpress, in order to display a nested repeater ACF field, I adapted a few lines in php that I insert in the archive page of post.
In the archive, the hard code runs very well and the lines created by the code are displayed as expected.
I need to use this code via Elementor. To do that, I need to create a shortcode.
I created a shortcode with the code.
Nothing runs good, nothing is displayed, even when I desactive Elementor
Can you, please, help me,
Bruno
Here is the code:
function acf2repeaters_ingredients_function() {
while ( have_posts() ) : the_post();
// check for rows (parent repeater)
if( have_rows('en-tete_ingredients') ): ?>
<div id="en-tete_ingredients">
<?php // loop through rows (parent repeater)
while( have_rows('ingredients') ): the_row(); ?>
<div>
<h3><?php the_sub_field('en-tete_ingredients'); ?></h3>
<?php // check for rows (sub repeater)
if( have_rows('liste_des_ingredients') ): ?>
<ul>
<?php // loop through rows (sub repeater)
while( have_rows('ingredients') ): the_row(); ?>
<li <?php echo ' class="nombre"'; ?>><?php the_sub_field('quantite');?></li>
<li <?php echo ' class="unite"'; ?>><?php the_sub_field('unite');?></li>
<li <?php echo ' class="ingredient"'; ?>><?php the_sub_field('ingredient');?></li>
<?php endwhile; ?>
</ul>
<?php endif; //if( get_sub_field('ingredients') ): ?>
</div>
<?php endwhile; // while( has_sub_field('en-tete_ingredients') ): ?>
</div>
<?php endif; // if( get_field('en-tete_ingredients') )
endwhile; // end of the loop.
}
add_shortcode( 'acf2repeaters_ingredients', 'acf2repeaters_ingredients_function' );
Solution:
Remove line: while ( have_posts() ) : the_post();
Remove line: endwhile; // end of the loop.
Then the shortcode rocks!
We have ACF Pro for WP and we have created an ACF which show a location which is a select.
When trying to output we are getting this:
Notice: Trying to get property of non-object in
/home/cwplantactiveint/public_html/wp-content/themes/cwplant/loop-jobs.php
on line 66
Which is this
<?php $location = get_field('job_location'); echo $location->post_title; ?>
Now oddly, it outputs another custom field which was createdto output the date:
<?php if(get_field('closing_date')) { ?>
<?php the_field('closing_date'); ?>
<?php } else { ?>
Ongoing
<?php } ?>
The whole code block looks like this:
<?php while ( have_posts() ) : the_post(); ?>
<?php /* Check closing date is not past. */
$today = strtotime("now");
$closedate = strtotime(get_field('closing_date'));
if ($today < $closedate || !get_field('closing_date')) {
?>
<div class="singlepost infobox info-job content cfix">
<h2><?php the_title(); ?></h2>
<p><span class="red first">Location:</span> <?php $location = get_field('job_location'); echo $location->post_title; ?>
<span class="red">Closing Date:</span>
<?php if(get_field('closing_date')) { ?>
<?php the_field('closing_date'); ?>
<?php } else { ?>
Ongoing
<?php } ?>
</p>
<?php if ( is_archive() || is_search() || is_home()) : // Only display excerpts for archives and search. ?>
<?php the_excerpt(); ?>
<a class="button" href="<?php the_permalink(); ?>">View Details</a>
<?php else : ?>
<?php the_content( __( 'Continue reading →', 'twentyten' ) ); ?>
<?php endif; ?>
</div>
<?php $jobstrue = 'true'; ?>
<?php } else { ?>
<?php $jobsfalse = 'true'; ?>
<?php } ?>
<?php endwhile; // End the loop. Whew. ?>
I think your problem is that you haven't reset the $post object with wp_reset_postdata() so your query is returning the last thing in $post (likely createdto in your case).
Whenever I handle any object in WP, I always set it up and then reset it like this:
<?php
// YOUR CUSTOM FIELD
// 0- Reset post object from last query (this should really be part of your last query)
wp_reset_postdata();
// 1- Put custom field into post object and check for content
$post_object = get_field('location');
// 2- Check to make sure object exists and setup $post (must use $post variable name)
if ($post_object) {
$post = $post_object;
setup_postdata($post);
// 3- Do some magic
echo get_field('closing_date');
// 4- reset postdata so other parts of the page can use it
wp_reset_postdata();
}
?>
I have created two php files (recent_blog_post.php and single_blog_post.php) one for displaying post from wordpress site within same domain and another for displaying single post. I don’t know whether it is possible or not. The first one works fine but the problem is if i put the_permalink() in the title or read more link, the page redirects to main wordpress site. I don’t want to go back to main site, want linking to the custom php file to show each single post. Here is my code for recent_blog_post.php
<ul>
<?php
require($_SERVER['DOCUMENT_ROOT'] . '../wordpress/wp-load.php');
$args = array(
'posts_per_page' => 5
);
$latest_posts = new WP_Query( $args );
if ( $latest_posts->have_posts() ) {
while ( $latest_posts->have_posts() ) {
$latest_posts->the_post(); ?>
<li>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php }
} else {
echo '<p>There are no posts available</p>';
}
wp_reset_postdata();
?>
</li>
</ul>
And single_blog_post.php
<?php require($_SERVER['DOCUMENT_ROOT'] . '../wordpress/wp-load.php');
query_posts('showposts=1');
if(have_posts()) : while(have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content();?>
<?php edit_post_link('Edit', '<p>', '</p>'); ?>
<?php endwhile; ?>
<?php endif; ?>
you have to use the filter "the_permalink"
function subs_url ( $url ) {
return str_replace ( "www.mywebsite.com/", "www.mywebsite.com/single_blog_post.php/", $url );
}
add_filter('the_permalink', 'subs_url');
That filter will change your permalink from www.mywebsite.com/category/slug to www.mywebsite.com/single_blog_post.php/category/slug
Source: https://codex.wordpress.org/Plugin_API/Filter_Reference/the_permalink
Quasi-noob here. I just can't get any result from get_field. Here's my test page:
http://23.21.199.240/test
I can retrieve the repeater field data with a FOR loop, but the special ACF get_field function isn't doing what I expected. (It seems so simple, so I won't be surprised if I'm making a totally bonehead mistake.)
Any help would be greatly appreciated!
<?php
/*
Template Name: test
*/
?>
<?php get_header(); ?>
<?php query_posts('category_name=worldwise&posts_per_page=3'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
id: <?php the_ID(); ?><br />
<!--let's try to display all the topics (an ACF repeater field) for this post....-->
<?php if(get_field('topic')): // if there are topics, get all their data in an array ?>
<?php
$topic_list_1 = ''; // set up an empty topic list
while(has_sub_field('topic')): // for every topic
$topic_title_1 = get_sub_field('topic_title'); // get the title
$topic_list_1 .= '<li>' . $topic_title_1 . '</li>'; // and add it to the topic list
endwhile; // no more topics, move on
?>
<p><strong>The FOR loop produced these topics:</strong></p>
<ul><?php echo $topic_list_1; ?></ul>
<?php else: ?>
<p style="color:red">GET_FIELD did not find any topics</p>
<?php endif; ?>
<!--or, let's try displaying those topics another way....-->
<?php
$vid_id = $post->ID;
$vid_custom = get_post_custom($vid_id);
?>
<?php if($vid_custom['topic'][0] != null): ?>
<?php
$topic_list_2 = '';
for($r=0;$r<$vid_custom['topic'][0];$r++):
$topic_title_2 = $vid_custom[topic_.$r._topic_title][0];
$topic_list_2 .= '<li>' . $topic_title_2 . '</li>';
endfor;
?>
<p><strong>The FOR loop produced these topics:</strong></p>
<ul><?php echo $topic_list_2; ?></ul>
<?php else: ?>
<p style="color:red">The FOR loop did not find any topics</p>
<?php endif; ?>
<br />
<?php endwhile; else: ?>
<p>Sorry, no posts or pages matched your criteria.</p>
<?php endif; ?>
I don't like using get_sub_field methods as it gets a bit complicated when you're nesting repeater fields.
This is what's in my opinion the easiest way to get what you're trying to do:
<ul>
<?php foreach (get_field('topic') as $row) :?>
<li><?php print $row['topic_title'] ?></li>
<?php endforeach; ?>
</ul>
i am using page.php only which is default template for pages in wordpress, my header footer and sidebar are same on all pages but main area content is changing which i want wordpress to pull for each specific page. for example if page is about us ..it should show posts of about us category and so on...
i want to know if i can achieve this with conditional statements or i have to make separate templates for each page.
i tried something like ...
<?php if (page_id ==11 ):
// post loop to display all fetch all post of category id 4
$page_query = new WP_Query('post_type=post&cat=4'); ?>
<?php while ($page_query->have_posts()) :
$page_query->the_post(); ?>
<?php the_title(); ?>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail');
} ?>
<?php the_content(); ?>
<?php endwhile; endif;
wp_reset_postdata();
?>
this is my loop on page.php but it doesn't seem to work!
wow... not sure if this helps, but is much neater and cleaner than what you have currently
<?php
if (page_id == 11){
$page_query = new WP_Query('post_type=post&cat=4');
while ($page_query->have_posts()){
$page_query->the_post();
echo "<".the_title()."";
if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail');
}
the_content();
}
}
wp_reset_postdata();
?>
Still, you're unclear of what does not work, so it's hard to tell what isn't working...
I see this has never been resolved and I had nothing to do so I hope this will help someone in the near future.
If you want to show posts that are in relation to the page "about us" you should name that categorie the name of the page. By doing so you you can change to code above to the following:
global $post;
$pagename = get_the_title($post->ID);
$args = array(
'post_type' => 'post',
'category_name' => $pagename,
);
$page_query = new WP_Query($args);
while ( $page_query->have_posts() ) : $page_query->the_post();?>
<a href="<?php the_permalink(); ?>" rel="bookmark">
<?php the_title()?>
</a>
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail');
}
the_content();
endwhile;