I am trying to add a custom field value to related posts functionality, but am currently totally stuck at this:
<div class="relatedposts">
<h3>Related posts</h3>
<?php
$orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>4, // Number of related posts to display.
'caller_get_posts'=>1
);
$my_query = new wp_query( $args );
while( $my_query->have_posts() ) {
$my_query->the_post();
?>
<div class="relatedthumb">
<a rel="external" href="<? the_permalink()?>">
<?php
$image = wp_get_attachment_image_src(get_field('image'), 'full');
print_r( $image[0] ); ?>
<?php the_title(); ?>
<img src="<?php echo $image[0]; ?>" />
</a>
</div>
<? }
}
$post = $orig_post;
wp_reset_query();
?>
</div>
This mostly works as it is code found online, it is just where to put the reference to the custom field that I seem to be missing. I am unable to place the variable and print_r of this anywhere to see the results.
foreach($tags as $individual_tag) {
$meta_values = get_post_meta($post->ID,'');
if (in_array('VALUE SEARCHING FOR',$meta_values) {
$tag_ids[] = $individual_tag->term_id;
} // if
} // foreach
Fill in the appropriate blanks and this should filter your tags properly.
HTH,
=C=
With ACF, you can pass a post ID to the get_field and the_field functions:
$image_id = get_field( 'image', $post -> ID );
If you use the functions without a set post ID, the functions may return the field value of some other globally initialized post.
So your attachment image getter (inside the related posts WP_Query loop) would be something like
$image = wp_get_attachment_image_src(get_field('image', get_the_ID()), 'full');
Make sure your ACF field type is set to image and the wanted variant of it: ID, image object, or image URL. get_field will then return either the image's attachment ID, the image object or the image URL.
In your code example, returning an ID would be the correct choice, assuming wp_get_attachment_image_src expects an attachment ID.
EDIT:
I just realized, that the ACF image field may not be saving the images to the WP attachment system in a normal way WP does. Try returning the image as an object and var_dump the object's contents to see where the full size image URL resides there.
I could be wrong and the wp_get_attachment_image_src might work fine as is.
Ok, anyone who may find this useful, I have a solution for getting the ACF field to appear:
<div class="related grid clear">
<h2>Related posts</h2>
<?php
$orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>9,
'caller_get_posts'=>1
);
$my_query = new wp_query( $args );
if($my_query->have_posts()) {
while($my_query->have_posts()) {
$my_query->the_post();
$image = get_field('hero_image', get_the_ID());
$introduction = get_field('introduction');
?>
<article class="grid-item" data-permalink="<? the_permalink()?>">
<div style="background-image: url(<?php echo $image; ?>);"></div>
<h3><?php the_title(); ?></h3>
<p><?php echo $introduction; ?></p>
</article>
<?
}
}
$post = $orig_post;
wp_reset_postdata();
wp_reset_query();
}
?>
</div>
Related
My posts get ordered by date which is picked by an advanced custom field datepicker. I want to use the regular Wordpress function references [the_title(), etc …] and the post related custom field's.
Right now the output of every loop is the the same. I read setup_postdata() can solve this issue and enable the use of the regular function references. I tried to apply it, but the output keeps being always the same. Thanks
<?php
global $posts;
$posts = get_posts(array(
'post_type' => 'post',
'meta_key' => 'release_date',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
$group_posts = array();
if( $posts ) {
foreach( $posts as $post ) {
$date = get_field('release_date', $post->ID, false);
$date = new DateTime($date);
$year = $date->format('Y');
$month = $date->format('F');
$group_posts[$year][$month][] = array($post, $date);
}
}
foreach ($group_posts as $yearKey => $years) {
foreach ($years as $monthKey => $months) {
echo '<li class="time">' . $monthKey . ' ' . $yearKey . '</li>';
foreach ($months as $postKey => $posts) {
setup_postdata($posts); ?>
<li class="item clearfix">
<!-- Wordpress Functions -->
<?php the_title();?>
<?php the_permalink();?>
<!-- Advanced Custom Fields -->
<?php the_field('blabla')?>
</li>
<?php
}
}
} wp_reset_postdata();
?>
You just need to add a line $post = $current_post; just before calling setup_postdata( $post ). See my example below to have a clear idea:
$posts = get_posts(array(.......));
// Call global $post variable
global $post;
// Loop through sorted posts and display using template tags
foreach( $posts as $current_post ) :
//the below line is what you missed!
$post = $current_post; // Set $post global variable to the current post object
setup_postdata( $post ); // Set up "environment" for template tags
// Use template tags normally
the_title();
the_post_thumbnail( 'featured-image-tiny' );
the_excerpt();
endforeach;
wp_reset_postdata();
For details please see this comment posted in the WP-Codex;
I am using a simple wordpress shortcode
function my_recent_post()
{
echo 'hello';
}
add_shortcode( 'recent', 'my_recent_post' );
with the shortcode [recent] and its working fine and visible in front page,
but the problem is, its printing the hello in the dashboard also.
below is the screenshot, can anyone please help.
Update:
I was actually trying to show posts, so can you help me with this, because it renders the lists of posts in the dashboard itself like the "hello". I tried:
function lorem_function() {
global $post;
$args = array( 'posts_per_page' => 10, 'order'=> 'ASC', 'orderby' => 'title' );
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post ); ?>
<div>
<?php the_date(); ?> <br /> <?php the_title(); ?> <?php the_excerpt(); ?>
</div>
<?php endforeach;
wp_reset_postdata();
return;
}
add_shortcode('lorem', 'lorem_function');
Based on your comments to me & Nikita Dudarev, what you need to do is create a variable to hold all the post information and then return it. Using the function you posted as an example:
function lorem_function() {
global $post;
$args = array( 'posts_per_page' => 10, 'order'=> 'ASC', 'orderby' => 'title' );
$postslist = get_posts( $args );
// create a variable to hold the post information
$html ="";
foreach ( $postslist as $post ) :
setup_postdata( $post );
$backgroundstyle = "";
// get the featured image and set it as the background
if ( has_post_thumbnail() ) { // make sure the post has a featured image
$imageurl = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium' ); // you can change "medium" to "thumbnail or full depending on the size you need
// add the css for the background image. You can include background-size etc ad required
$backgroundstyle = "background-image: url('".$imageurl[0]."');";
}
// add the information to the variable
$html .= '<div style="'.$backgroundstyle.'">';
$html .= get_the_date();
$html .= "<br />";
$html .= get_the_title();
$html .= get_the_excerpt();
$html .= "</div>";
endforeach;
wp_reset_postdata();
return $html;
}
add_shortcode('lorem', 'lorem_function');
Note that the_date(), the_title() and the_excerpt() all display the information (just like echo).
Instead you must use get_the_date(), get_the_title() and get_the_excerpt() - these get the same information, but instead of displaying it directly, they return it as a variable which you can then store in your html string to be returned.
Update:
As you don't want to use the variable name on each line for whatever reason, you can do it like this:
$html .= "<div>".get_the_date()."<br />".get_the_title().get_the_excerpt()."</div>";
I'm not sure why you specifically want to change it to do that - it makes absolutely no difference to how it works, it just makes it harder to read and identify any errors :-)
Your function must return a value, not output
function my_recent_post()
{
return 'hello';
}
add_shortcode( 'recent', 'my_recent_post' );
I have a custom post type called press.
What I want to achieve is generate a list of the titles (with it's corresponding link to it) of all press custom post types.
I tried with this code with no luck, any ideas?
<?php function all_posts_custom_posts( $query ) {
$post_type = $query->query_vars['post_type'];
if ( 'press' == $post_type ){
$query->query_vars['posts_per_page'] = -1;
return;
}
}
add_action('pre_get_posts', 'all_posts_custom_posts',1); ?>
And also highlight the current post from the list by adding a class to it.
I had a similar issue sometime ago. The following code example will display how I resolved my issue.
<?php
$type = 'products';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
I'm having troubles retrieving the thumbnail of each post contained in an array.
I have this array that contains every post of a custom post type:
<?php
$clients_array = array(
'post_type' => 'clients',
'sort_order' => 'ASC',
'sort_column' => 'post_title',
'post_status' => 'publish'
);
?>
While I have no problem retrieving the thumbnail using the standard wordpress loop, like this:
<?php
$query = new WP_Query( $clients_array );
while ( $query->have_posts() ) : $query->the_post();
?>
<?php if ( has_post_thumbnail()) : ?>
<?php the_post_thumbnail() ?>
<?php
endif;
endwhile;
?>
I'd like to load the posts with a foreach look, such as:
<?php
$clients = get_pages($clients_array);
foreach ($clients as $page_data) {
$client_id = $page_data->ID;
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($client_id), 'thumbnail' );
echo $thumb;
}
?>
Unfortunately, I can't get it work in any way I tried.
What am I doing wrong?
Most of WordPress's functions prefixed with get_ are to retrieve the specified data and not echo it. Therefore putting the data into a variable or echoing it manually would work for your situation like #jothikannan said:
echo get_the_post_thumbnail($id);
or
$foo = get_the_post_thumbnail($client_id);
//do sowething with $foo
you must use follow to get the thumbnail of the features image
<?php echo get_the_post_thumbnail($client_id); ?>
it is already answered here look at here
I'm getting the word "Array" coming up on my blog POSTS page when I use the below code. I have a feeling that it relates to this line:
I am using a similar template for my PORTFOLIO and NEWS pages where the wording is slightly different on this line ("showportcat" and "shownewscat" instead of "get_the_category") and it should be displaying the categories but instead, the word 'array' is in it's place.
I've tried "showpostcat" but that didnt' work so I wonder if I need to be re-wording it? Or maybe the problem is on another part of the code which I've included below?
<div class="greyblock">
<h4 class="nomar"><?php echo the_title(); ?></h4>
<div class="sep"></div>
<?php echo get_the_category(get_the_ID()); ?>
<div class="clear"></div>
<ul class="blogpost_list columns2">
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$args = array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => get_option("posts_per_page"),
);
if (isset($_GET['slug'])) {
$args['tax_query']=array(
array(
'taxonomy' => 'postcat',
'field' => 'slug',
'terms' => $_GET['slug']
)
);
}
$wp_query->query($args);
?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post();
#We have:
#get_permalink() - Full url to post;
#get_the_title() - Post title;
#get_the_content() - Post text;
#get_post_time('U', true) - unix timestamp
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
echo "
<li class='pc'>
<img alt='".get_the_title()."' src='".TIMTHUMBURL."?w=400&h=400&src=".$featured_image[0]."'>
<h4>".get_the_title()."</h4>";
$terms = get_the_terms($post->ID, 'postcat');
if ( $terms && ! is_wp_error( $terms ) ) {
$draught_links = array();
foreach ( $terms as $term ) {
$draught_links[] = $term->name;
}
$on_draught = join( ", ", $draught_links );
}
echo "
<p>".get_the_excerpt()."</p>
<a href='".get_permalink()."' class='read'>Read more</a>
<br class='clear' />
</li>
";
endwhile; ?>
</ul>
</div>
<?php get_pagination() ?>
<?php $wp_query = null; $wp_query = $temp; ?>
How about:
$categories = get_the_category(get_the_ID());
foreach ($categories as $category) {
echo $category;
}
The problem is still there. I'm guessing it relates to the way I am wording one of the below two lines as it is 'showportcat' for portfolios and 'shownewscat' for news but 'showpostcat' doesn't work for posts so I just can't figure it out:
<?php echo get_the_category(get_the_ID()); ?>
**OR**
'taxonomy' => 'postcat',