Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMaja B.
12,984 PointsWordPress - Echo something just for chosen posts
Hi, is it possible in Wordpress to be inside a loop and say *If post_id is 7,10, 102 that write the following <a href=#>Read more</a>
Please see this sentence below in the code. There I need a special note but just for some posts.
Is it possible to do that? Its a loop inside a loop.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="grid-container">
<!-- Primary Column -->
<div class="grid-4">
<?php the_field( 'description' ); ?>
If post_id is 7,10, 102 that write the following
<a href=#>Read more</a>
</div>
<!-- Secondary Column -->
<div class="grid-8">
<?php the_field( 'project_images' ); ?>
</div>
</div>
<?php endwhile; endif; ?>
2 Answers
Andrew Shook
31,709 PointsMaja, yes it is possible, but I'm not sure why you need to target these pages specifically. Could you explain the rationale behind way you need to do this?
In the mean time, you can check to see if a given post matches a post ID by using is_page, for pages only, and is_single, for everything except pages and attachments. Try this code:
<?
// both is_page and is_single will accept an array, so we will store the special page id's in an array
$special_pages = array(7, 10, 102);
php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="grid-container">
<!-- Primary Column -->
<div class="grid-4">
<?php the_field( 'description' ); ?>
<?php if( is_page($special_pages) || is_single($special_pages) ): //this is statement is only pass is the post matches 7, 10, or 102 ?>
<a href='#'>Read more</a>
<?php endif;?>
</div>
<!-- Secondary Column -->
<div class="grid-8">
<?php the_field( 'project_images' ); ?>
</div>
</div>
<?php endwhile; endif; ?>
Baruch Velez
275 PointsThe beauty of WordPress is the amount of built-in functions that make everyday problems have an easy solution. One of these functions is the get_the_excerpt() function which is a word-counted trim for posts. So if the post has more than x amount of words then it gets trimmed, and you can use the function to display the trimmed part. The following code was taken from the WP Codex.
You can use the following:
<?php
$my_excerpt = get_the_excerpt();
if ( $my_excerpt != '' ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
Source: http://codex.wordpress.org/Function_Reference/get_the_excerpt
Edit: You can also use wp_trim_exceprt(); explained here http://codex.wordpress.org/Function_Reference/wp_trim_excerpt
Maja B.
12,984 PointsHi, Baruch, I've only seen your reply today.
Thanks for directing me also to the get_the_excerpt() function.
I think I will need it very soon :)
Maja B.
12,984 PointsMaja B.
12,984 PointsHi, the rational behind it is this: I'm making a portfolio page for my friend, a painter. The custom post items are his books. Some of these books can be bought at Amazon and elsewhere. Those will need a button that will say: Buy at Amazon or something like that. The others will not need that button. So what I need to do is to say to WordPress for the posts with an ID (or even better for the posts that have a custom filed value Buy checked as true, display the button, for others do not. Does it make sense?
Andrew Shook
31,709 PointsAndrew Shook
31,709 PointsSince you are already using Advanced Custom Fields, why don't you just make a field that will hold the url for the link. Then, check to if the field has text, and if it does print out a link. Try this:
Just replace "book_link" with whatever you name the field in ACF.
Maja B.
12,984 PointsMaja B.
12,984 PointsWhat an elegant solution! How come that I did not thought of it myself :))) I have quickly tested it in my code and it works. Great! Thanks!