I am using get_posts to retrieve posts information from database. It returns "Post title", "thumbnail", "post category", and "post excerpt". Everything is working fine but the problem is I am unable to show post excerpt.
Here is my code:
function widget ($args,$instance) {
extract($args);
$title = $instance['title'];
$catid = $instance['catid'];
$numberposts = $instance['numberposts'];
$date = $instance['date'];
$rss = $instance['rss'];
// retrieve posts information from database
global $wpdb;
$posts = get_posts('post_type=post&numberposts='.$numberposts.'&category='.$catid);
$out = '<ul>';
if ($posts) {
foreach($posts as $post) {
setup_postdata($post);
$out .= '<li>'.get_the_post_thumbnail($post->ID,'medium').'</li>';
$out .= '<li>'.$post->post_title.'</li>';
$out .= '<li>'.$post->post_excerpt.'</li>';
if ($date) $out .= '<li>'.date('d/m/Y', strtotime($post->post_date_gmt)).'</li>';
}
}
if ($rss) $out .= '<li>Category RSS</li>';
$out .= '</ul>';
//print the widget for the sidebar
echo $before_widget;
echo $before_title.$title.$after_title;
echo $out;
echo $after_widget;
}
}
$post->post_excerpt does not get work the way you think it does. Most people think this is the same as the template tag the_excerpt(), and it is not
the_excerpt() is generated by truncating get_the_content(). $post->post_excerpt is not generated at all as this is user defined. This excerpt is the excerpt text manually added by the user in the post edit screen in the excerpt meta box. (This meta box is hidden by default, but can be enabled in the "Screen Options" tab on the top part of the screen). If the user did not specify a manual excerpt, $post->post_excerpt will return nothing, that is why you see this behavior
You have already set up your postdata, so you can just simply use the template tags directly, so in place of $post->post_excerpt, you can use the_excerpt()
EDIT
Thanks to the comment below, I did not take into account that the excerpt should not be echo'd straight away. In this case, you would make use of get_the_excerpt() which does not echo the text, but simply retrieves it.
Related
I am using the WordPress Advanced Custom Field plugin. I have a custom built download button and shortcode. Now I am using ACF fields to populate the download btn shortcode. Everything seems to be working and download button is displaying as expected. The only problem is that ACF fields are not populating in the shortcode but its showing outside the shortcode code.
<?php
if (get_field('dn_btn_url1')) {
echo do_shortcode( '[download_btn url="'.get_field('dn_btn_url1').'" filename="'.get_field('dn_btn_txt1').'"]' );
}
?>
The code without the if statement is working as expected but it doesn't hide the shortcode.
The get_field function gets the post id from the global $post object so when it's passed into the shortcode function, it seems like it can't get the right post id. Try putting your URL from the field into a variable so the URL is already passed through the function and into the shortcode function.
Try this:
<?php
if (get_field('dn_btn_url1')) {
$url = get_field('dn_btn_url1');
echo do_shortcode( '[download_btn url="' . $url . '" filename="' . $url . '"]' );
}
?>
You can do that by using sprintf function please try it once
<?php
if(get_fiedl('dn_btn_url1')) {
$url = get_field('dn_btn_url1');
$filename = get_field('dn_btn_txt1');
$shortcode = sprintf(
'[download_btn url="'.$url.'" ,
filename="'.$filename.'"]');
echo do_shortcode( $shortcode );
?>
I'm using the below funky bit of code in my functions.php to enable the execution of PHP in my sidebar:
// 12. Execute/Parse PHP in Sidebar Content
// =============================================================================
add_filter('widget_text','execute_php',100);
function execute_php($html){
if(strpos($html,"<"."?php")!==false){
ob_start();
eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
I'm then using this code in my sidebar to reference a list of custom fields (related articles) that I want to list in the sidebar:
<?php $post_objects = get_field( 'related_article_list'); ?>
<?php if ( $post_objects) { ?>
<ul>
<?php foreach( $post_objects as $post): ?>
<?php setup_postdata($post); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php } ?>
Now this code works fine in my page template and correctly lists the three related articles that I have set in each post, but in the sidebar it just generates the current post title three times.
Any ideas much appreciated!
It is generally not good practice to place PHP code directly in a sidebar textfield then parsing/executing its content is leaves open to many potential problem.
Instead you should place the code inside a shortcode and then place that shortcode tag inside the Widget in question. [my_related_article_list]
add_shortcode('my_related_article_list','my_related_article_list_func');
function my_related_article_list_func(){
$post_objects = get_field( 'related_article_list');
$html = '';
if ( $post_objects) {
$html = '<ul>';
foreach( $post_objects as $post):
$html .= '<li>'.get_the_title($post).'</li>';
endforeach;
$html .= '</ul>';
}
return $html;
}
Concerning the fact that you see the current post three time in the sidebar has todo with the loop not have been properly set and 3 times because you probably also pulled the revisions.(i suppose)
Maybe executing the code in a shortcode could help improve your predicament in solving parts of the problem or even the whole problem.
Edit: Using the setup_postdata is unecessary since calling get_* functions which returns data instead of outputting it straight based on the current loop data set by setup_postdata. Therefor calling the get_* with the $post argument will return the appropriate context.
Solved by #Musk using the following shortcode:
add_shortcode('my_related_article_list','my_related_article_list_func');
function my_related_article_list_func(){
$post_objects = get_field( 'related_article_list');
$html = '';
if ( $post_objects) {
$html = '<ul>';
foreach( $post_objects as $post):
$html .= '<li>'.get_the_title($post).'</li>';
endforeach;
$html .= '</ul>';
}
wp_reset_postdata();
return $html;
}
I have a slight issue. I have managed, with the help of someone one stackoverflow to write a code to add dynamic content to all my wordpress posts. The code is now in my functions.php file and is as follows.
function add_after_post_content($content) {
global $post;
if(!is_feed() && !is_home() && is_singular() && is_main_query()) {
$post_categories = wp_get_post_categories( $post->ID );
$cats = array();
foreach($post_categories as $c){
$cat = get_category( $c );
$cats[] = array( 'name' => $cat->name, 'slug' => $cat->slug );
}
$content .= '<strong>'. $post->post_title . '</strong> is a <strong>wallpaper</strong> posted in the ';
foreach($cats as $c){
$content .= $c['name'];
}
$content .= ' category by <strong>Free Wallpapers</strong> on '. $post->post_date . '. <br /><br />';
}
return $content;
}
add_filter('the_content', 'add_after_post_content');
The issue is that some of my older posts already have content in them and hence both the old content as well as the new are displayed beneath them. whereas I would like this to be the new content/description for all posts. Currently in my content-single.php I believe this is the line which calls the original post content plus the code I have mentioned above.
<?php the_content(); ?>
I would like to wither remove or modify my content-single.php/code added to functions.php file so that only the new code displays the dynamic description, and the original post content is not displayed. i.e. putting it simply if the entire new code could be labeled content2 and the content-single.php code will output this content2 only. I have tried simply moving the code from functions.php to replace
<?php the_content(); ?>
in the content-single.php but this gives me all kinds of errors.
Any help on this issue would be greatly appreciated.
Best Regards
Remove the dot after $content.
$content .= '<strong>'. $post->post_title . '</strong> is a <strong>wallpaper</strong> posted in the ';
Replace above line with following
$content = '<strong>'. $post->post_title . '</strong> is a <strong>wallpaper</strong> posted in the ';
Here is what I'd like to accomplish
Goal A: I want to hyperlink each thumbnail in index.php to their post.
Goal B: I want to define a hyperlink for each thumbnail in single.php to an external website.
You may ask why am I using thumbnails for single.php? The reason is because I want this layout:
And so far I understand that there are 3 methods to display images:
Insert image into the editor area along with the text, but the problem is I cannot float the image and text differently because all items within a post are assigned a p tag - am I wrong?
Custom fields should get the job done but it doesn't seem the most efficient way - or am I wrong?
Post Thumbnails should be the easiest way but see my problem below
I have the code to accomplish Goal A and B but they only work separately.
In other words, "Code 1" does not work if "Code 2" is present.
How can I resolve this issue? Or is there a better method accomplish my goal?
Code 1: Link thumbnails to external websites using custom field (single.php)
<?php $name = get_post_meta($post->ID, 'externalurl', true);
if( $name ) { ?>
<?php the_post_thumbnail(); ?>
<?php } else {
the_post_thumbnail();
} ?>
Code 2: Link thumbnails to the post (functions.php)
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
function my_post_image_html( $html, $post_id, $post_image_id ) {
$html = '' . $html . '';
return $html;
}
is_single() function will help you achieve what you need. Try below code in functions.php and remove the additional code from single.php
function my_post_image_html( $html, $post_id, $post_image_id ) {
if ( is_single()) {
$name = get_post_meta($post_id, 'externalurl', true);
if( $name ) {
$html = '' . $html . '';
}
return $html;
}
else
{
$html = '' . $html . '';
return $html;
}
}
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
Okay, so you can add shortcodes to WP template files like:
<?php echo do_shortcode('[my_awesome_shortcode]'); ?>
but what if the shortcode is intended to wrap around content like this:
[my_awesome_shortcode]
Hey, this is the content within the awesome shortcode.
[/my_awesome_shortcode]
I'm a bit unsure how to put that into a template file.
The solution that worked for me was to combine shortcodes into a single string, so
<?php echo do_shortcode('[my_awesome_shortcode]<h1>Hello world</h1>[/my_awesome_shortcode]'); ?>
will work!
If you have a long chain of shortcode commands you want to execute, create a separate string for them like this
$shortcodes = '[row]';
$shortcodes .= '[column width="2/3"]';
$shortcodes .= 'Content';
$shortcodes .= '[/column]';
$shortcodes .= '[column width="1/3"]';
$shortcodes .= 'More Content';
$shortcodes .= '[/column]';
$shortcodes .= '[/row]';
Then execute the entire thing like this
<?php echo do_shortcode($shortcodes); ?>
According to http://codex.wordpress.org/Shortcode_API#Enclosing_vs_self-closing_shortcodes
adding $content = null to the shortcut function should do the trick:
function my_awesome_shortcode_func( $atts, $content = null ) {
return '<awesomeness>' . $content . '</awesomeness>';
}
add_shortcode( 'my_awesome_shortcode', 'my_awesome_shortcode_func' );
so that:
[my_awesome_shortcode]
Hey, this is the content within the awesome shortcode.
[/my_awesome_shortcode]
would result in:
<awesomeness>Hey, this is the content within the awesome shortcode.</awesomeness>