Displaying code snippets from WordPress custom fields - wordpress

I'm trying to display code snippets entered into my custom field. An example code snippet entered into custom field - snippet-1
<?php
if (($wp_query->current_post + 1) < ($wp_query->post_count)) {
echo '<div class="post-item-divider">Post Divider</div>';
}
?>
If I try to display this code wrapped in <pre></pre> tags in my page template like
<?php if ( get_post_meta($post->ID, 'snippet-1', true) ) : ?>
<pre><?php echo get_post_meta($post->ID, 'snippet-1', true) ?></pre>
<?php endif; ?>
but it returns nothing to the template. I understand WordPress is filtering the snippet out as it sees as PHP code to execute. Is there a way just to print this out on the page as a code snippet?
Many thanks in advance
rob

Use htmlspecialchars() to escape your code.
Update
echo htmlspecialchars(get_post_meta($post->ID, 'snippet-1', true));

Related

AFC - show files in comments

I'm trying to add feature to my website where users will be able upload files in comments.
I'm trying to do that using Advanced Custom Fields plugin.
Some parts of job are done. I have upload option while writing comment and after submiting comment the file is visable via Wordpress administration.
Comment administration (file is visable at the bottom)
https://i.imgur.com/zIyjy0M.jpg
Custom field settings
https://i.imgur.com/nqNQK5N.png
Code for frontend I'm using in comments template
<?php
$cfile = get_field('comment_file');
if( $cfile ): ?>
<?php echo $cfile['filename']; ?>
<?php endif;
?>
The problem is - I can't see file in frontend part of website. Like there is no file(s).
$comment = get_comment(); // Here you can get single Comment array
$cfile = get_field('comment_file', $comment); // Then you need to pass Comment variable with get_field
if( $cfile ): ?>
<?php echo $cfile['filename']; ?>
<?php endif;
You can visit below link for Reference - https://www.advancedcustomfields.com/resources/get-values-comment/

How to display list categories in wordpress integration magento

Can help somebody. I spent several hours to find solution but without results
I tried to display the list of categories on homepage wordpress blog thru following code
<?php $category = Mage::registry('wordpress_category') ?>
<?php if ($category): ?>
<?php echo $category->getId() ?>: <?php echo $category->getName() ?>
<?php endif; ?>
But the method
Mage::registry('wordpress_category')
always return null.
I found that, i should probably be using the Fishpig_Wordpress_Block_Category_View. But i dont know where i should put it.
The following code will retrieve the current category when viewing a category page in your blog:
<?php Mage::registry('wordpress_category') ?>
This is not what you need. To view a list of categories, you could create a custom collection using the following:
<?php $categories = Mage::getResourceModel('wordpress/post_category_collection') ?>
A better way would be to use the category widget block:
<block type="wordpress/sidebar_widget_categories" name="wp.categories" template="wordpress/sidebar/widget/categories.phtml" />
You can create this in PHP using the following code:
<?php echo Mage::getSingleton('core/layout')
->createBlock('wordpress/sidebar_widget_categories')
->setTemplate('wordpress/sidebar/widget/categories.phtml')
->toHtml() ?>
The above code uses the default template, however, feel free to use your own custom template.

Wordpress Shortcode in custom field within custom post type

Hi Guys Need help with this very badly.
Need to add shortcode to output in the area circled in white in the picture below.
And the input area is under video description. And from my understanding ive have confirmed that the name for that text area is description_value.
I have looked through every documentation and tried all filters and do_shortcode variations to no avail. Please help i have spent 3 days non stop doing this. Puting the codes in my function.php and so many others. It still does not parse [shortcodes] it just displays text "[shortcodes]". please refer to picture below
Thank you.
This is outputing on the page. I have
<div class="describe-feat">[postexpirator]</div>
This is in a file called grid-gallery.js
<h2><%= item.title %></h2></div><div class="view-gallery">\
<div class="describe-feat"><%=item.desc%></div>\
<% if(item.imgnum){ %><span class="item-num"><%= item.imgnum %></span><% } %>\
This is in custom post editor in wordpress admin area
<textarea name="description_value" class="option-textarea">[postexpirator]</textarea>
https://www.dropbox.com/s/almn09e1dwmeywb/shortcodxe.jpg
It's because, you are missing do_shortcode function for parsing shortcode.
Assuming you just want to target a single value, you could just do this inside the loop.
<?php echo ( do_shortcode( get_post_meta( $post->ID , 'Your textarea Key Name' , true ) ) ); ?>
If your post has multiple values for that custom field, then you can set the above to false.. and loop over the array...
<?php $values = do_shortcode( get_post_meta( $post->ID , 'Your textarea Key Name' , false ) ); ?>
<?php if($values && is_array($values)) : ?>
<?php foreach( $values as $meta) : ?>
<p><?php echo $meta ?></p>
<?php endforeach; ?>
<?php endif; ?>
Just want to add that if you won't use the_post() no shortcode will work,
I had this issue when trying to enable shortcode on CutomFields on a new page type, and nothing worked until activating WP loop with the_post() .

Display specific post format with limit number of post

I create a loop in Wordpress with the condition like this:
Display a specific post format (example: video)
Limiting the post number. In this case, I only want to display 2 posts.
Here my code:
<?php $i = 1; if (have_posts()) : while (have_posts() && $i < 3) : the_post(); ?>
<?php get_template_part( 'content-video', get_post_format() ); ?>
<?php $i++; endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
I already have file name content-video.php.
Unfortunately, the loop does not work. Only displayed the first post, not a specific post (video post format) with template from content-video.php.
Appreciate for any help, or alternative code. Thanks in advance.
My remarks about your code:
I think you have bad wrapping for if else while statements. You are missing an endif at the end in the case your approach is correct.
Why use inside your code the i variable since you can customize any query in WP, especially for number of post using the parameter posts_per_page.
Why not to use the loop inside content-video.php and write only:get_template_part('content-video', get_post_format()); Like in single.php and loop-single.php the themes provided with wordpress installation (twenty themes) .
Good luck
the best way would be to get posts of "video" post format from a query, but I'm not sure how:)
As a workaround you could do this inside the loop:
if (!has_post_format('video'))
continue;

Display all comments for a WordPress blog on a single page?

What I need - The code to perform the following:
I am trying to setup a WordPress template that will display all the comments that have been posted to my blog. How do I pull all comments and have all the same formatting that is applied to comments under a single post? Such as the formatting that occurs when comments are displayed using the comments.php template.
Note I want to pull all the comments from my blog to a single page. I still want the comment pagination but instead of having 20 comments under post #1, 20 under post #2, etc. I want to have all 40 show up at one time on one page.
You want to use the get_comments() function.
<?php foreach (get_comments() as $comment): ?>
<div><?php echo $comment->comment_author; ?> said: "<?php echo $comment->comment_content; ?>".</div>
<?php endforeach; ?>
See also the apply_filters() function to apply comment output filters to specific fields.
<?php echo apply_filters('comment_text', $comment->comment_content); ?>
EDIT:
For pagination, you can use the offset and number parameters of the get_comments() arguments:
<?php
$args = array(
'number'=>20,
'offset'=>0,
'status'=>'approve',
);
foreach (get_comments($args) as $comment) {
// ...
}
?>

Resources