AFC - show files in comments - wordpress

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/

Related

Angular + WordPress

I have a ticket sales form written in Angular. How can I insert this form on the main page of Wordpress site? May be with usingthe tag iframe or can there be better solutions for this task? Thank you for attention!
I think a best solution to this is to create a new page template (you can google it to get more accurate info), see what is page templates here.
Basically, you will create a new file on your theme root, with the prefix page-, e.g: page-tickets.php. And then the very first thing that must appear on your page is a comment,like this: // Template Name: Tickets Page. Your page will be something like this:
<?php
// Template Name: Tickets Page
get_header();
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
// Angular + Html codes here!
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<script>angular and js imports</script>
<?php get_footer(); ?>
With that, when you will create a new page in Pages -> New (or edit), in the right bottom side will be available an option to you to choose a page template, like this:
That's all, I hope it helps you.

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.

Password protecting not working for custom post type single-template.php? - Wordpress

I have created a custom post-type 'Clients' where admin user can create new clients, add pictures and details to post, then password protect the page so only a particular client can access the content.
For displaying content of this post-type on the front end, I'm using a single-clients.php template. It displays the content perfectly, but the password protect function does not display the form and hide the content, even if I'm in a different browser, cache cleared/logged out of Wordpress (viewing it as a regular end-user would).
What might I be doing wrong here?
<?php get_header(); ?>
<div class="container-client">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
Display all fields and content for post-type
<?php endif; ?>
<?php endwhile; else: ?>
<div class="alert-box error">Sorry, this page no longer exists :( — Back to home</div>
<?php endif; ?>
</div>
<?php get_footer(); ?>
This is roughly how my single-clients.php page is setup. Is there any way to manually display the password function so that when end-user visits page, the content is hidden and password form is displayed?
I had exactly this problem and after some trying and reading the codex I came up with this solution:
<?php
add_filter('single_template', function($single_template) {
global $post;
if ($post -> post_type == 'your-custom-post-type') {
if (!post_password_required()) {
$single_template = 'path-to-your/single-your-custom-post-type.php';
}
}
return $single_template;
});
?>
This way the page is only rendered in the custom sinlge view after entering a password, if it is password protected.

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) {
// ...
}
?>

Displaying code snippets from WordPress custom fields

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

Resources