Parsing PHP in Sidebar Issue - php

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;
}

Related

How to apply excerpt inside my XML data on WordPress

I'm pulling out the data from my CRM to my WordPress site using a XML.
It worked actually, but I want to add some functions like read more with the my XML data {Web_Remarks[1]} it contains a long description.
Then I tried to add conditions from from my function.php
like excerpt and edit my descriptions code.
For my functions.php I add
<?php
// Customize excerpt for description word count length
function custom_excerpt_length(){
return 25;
}
add_filter('excerpt_length','custom_excerpt_length');
?>
And for my description.php I change my <?php the_content();?> to
<?php if ($post->post_excerpt) { ?>
<p>
<?php echo get_the_excerpt(); ?>
Read MoreĀ»
</p>
<?php }else{
the_content();
}
?>
I expect the output should be "3Bed room with laundry ro..Read More..."
but what I have now is
"3Bed room with laundry room and sea view with 2 car parkings only for 3.06M!
-Bright
-Spacious
-Sea/Palm view
-Balconies
-Higher floor
-No Construction chance infront
-Prime location
real estate are a Property Investment Firm with an ownership of more than 2500 properties all around the world.These are handpicked exclusive apartments and villas, located within the most prestigious and high-profile developments of Dubai. We do not just believe in customer satisfaction, we aim for customer delight. We understand that our customers define the standard of quality and service and your loyalty must be earned."
Try,
<?php
$excerpt = get_the_excerpt();
if( !empty($excerpt) ) { ?>
<p>
<?php echo $excerpt; ?>
Read MoreĀ»
</p>
<?php
} else {
the_content();
}
Or this will be a another method you can use:
<?php
// Add this code in functions.php file
function wp_get_custom_excerpt( $limit = '25' ) {
global $post;
$content = get_the_excerpt();
if( empty($content) ) {
$content = strip_shortcodes( $post->post_content );
}
$excerpt = wp_trim_words( $content, $limit, '[...]' );
return $excerpt;
}
// Remove your excerpt code and just echo function.
echo wp_get_custom_excerpt();

How to use a template or partial inside a loop in a shortcode WordPress?

To organize better my code, I would like to split some of my HTML in templates.
I have a loop inside a shortcode to get the posts:
function nh_shortcode_func($atts) {
$posts = get_posts_by_category(.......);
$html .= '';
foreach ($posts as $post) {
$post_id = $post->ID;
$post_title = $post->post_title;
$post_image = get_field('image', $post_id);
$html .= **HERE THE CODE OF HTML WITH DIVS AND CLASSES**
}
return $html
}
add_shortcode('nh-content', 'nh_shortcode_func');
Instead, put my HTML code inside a variable, I would like to use another file with the HTML structure that I'm going to use to build the posts. Also, I would like to pass the dynamic data to it.
Any ideas?
Thanks.
Just use an include file and it will have access to the variables. Something like:
function nh_shortcode_func($atts) {
ob_start();
include dirname( __FILE__ ) . '/templates/nh_shortcode.php';
return ob_get_clean();
}
add_shortcode('nh-content', 'nh_shortcode_func');

WordPress Post Excerpt is not showing in my Recent Post Widget

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.

How to create a shortcode for Admin Theme Option - Wordpress

I am trying to create a shortcode for 2 of my theme's options. Basically I want to grabe my theme options to my shortcode so that I can display them anywhere on my posts something like this:
[ads_1]
[ads_2]
However when I tried to get the options on my shortcode it wont work at all and its giving me an error
Parse error: syntax error, unexpected 'theme_settings' (T_STRING) in C:\xampp\htdocs\themewp\wp-content\themes\bots_final\shortcodes.php on line 86
Here's my snippet for my shortcode where in I am trying to grab my theme option data:
add_shortcode('ads_1', function($atts){
return '
<div>
<?php $options = get_option( 'theme_settings' ); ?>
<?php echo $options['banner1']; ?>
</div>';
});
I am trying to grab the options from my Theme options page. Check my theme options code:
<div>
<?php $options = get_option( 'theme_settings' ); ?>
<?php echo $options['banner1']; ?>
</div>
Any idea what's the problem why I can't grab it?
You opened the <?php inside an open php tag:
add_shortcode('ads_1', function($atts){
$html = '<div>';
$options = get_option( 'theme_settings' );
$html .= $options['banner1'];
$html .= '</div>';
return $html;
});
What do you get from get_option( 'theme_settings' );? It seems like your suggesting an array..
To avoid conflicts, I'd store your settings as 'mytheme_theme_settings'.

Drupal panels, 'edit panel' tabs is missing

I noticed something odd when I switch themes. In garland, I can see the view & 'edit panel' tab buttons; but when I switch back to my custom theme, it disappears.
I already have the tabs line:
<?php if ($tabs): print '<ul class="tabs primary">'. $tabs .'</ul></div>'; endif; ?>
But it's just not appearing. Why is that?
Here is some of the code for page.tpl.php:
<div class="main-container">
<div class="mcontent">
<div id="content-header">
<?php if ($mission): print '<div id="mission">'. $mission .'</div>'; endif; ?>
<?php if ($tabs): print '<div id="tabs-wrapper" class="clear-block">'; endif; ?>
<?php if ($title): print '<h2'. ($tabs ? ' class="with-tabs"' : '') .'>'. $title .'</h2>'; endif; ?>
<?php if ($tabs): print '<ul class="tabs primary">'. $tabs .'</ul></div>'; endif; ?>
<?php if ($tabs2): print '<ul class="tabs secondary">'. $tabs2 .'</ul>'; endif; ?>
<?php if ($show_messages && $messages): print $messages; endif; ?>
<?php print $help; ?>
</div> <!-- /#content-header -->
<?php print $content; ?>
</div>
</div>
I've used that code in quite a few custom themes without any issues so far.
It's hard to know without seeing the site/your code, but a couple of possibilities:
clearing your cache (Performance > Clear site cache)
making sure the $tabs variable is in page.tpl.php, not node.tpl.php etc.
Other things it could be (but maybe not if other themes are working):
your user doesn't have permissions to use that input format (PHP code, Full HTML etc)
the session isn't working or you've been logged out while on those pages (of course you will see this right away if, for example, your admin menu shows up. I've seen it happen on some setups, though)
Usually, either the code is in the wrong place, it's being hidden/obscured (by CSS/page formatting), or the person doesn't have the permissions to view it.
If none of the above work, you may want to rebuild permissions (in Drupal 6, Content Management > Post Settings > Rebuild Permissions), or play with enabling existing permissions to see if that's the culprit.
I hired someone to fix the issue, here is what is done:
<?php
/**
* Override of theme_menu_local_tasks().
* Add argument to allow primary/secondary local tasks to be printed
* separately. Use theme_links() markup to consolidate.
*/
function kidstoria_menu_local_tasks($type = '') {
if (module_exists('ctools')) {
if ($primary = ctools_menu_primary_local_tasks()) {
$primary = $primary;
}
if ($secondary = ctools_menu_secondary_local_tasks()) {
$secondary = $secondary;
}
}
else
{
if ($primary = menu_primary_local_tasks()) {
$primary = $primary;
}
if ($secondary = menu_secondary_local_tasks()) {
$secondary = $secondary;
}
}
switch ($type) {
case 'primary':
return $primary;
case 'secondary':
return $secondary;
default:
return $primary . $secondary;
}
}

Categories