• Resolved clairedelune

    (@clairedelune)


    Firstly, I am not a developer I am a graphic designer. Help please.

    Recently I customised a code snippet that I found on wpbeginner.com and after a few trial and error approaches I got it working just fine.

    The Code Snippets function lists the specified postsbycategory on the page where the shortcode is inserted, initially no comments nor the response box displayed because comments are disabled on the page.

    However, after recent updates to WP and plugins now it displays the comments forms and responses on the last post in the results displayed on the page.

    How can I exclude the response box and fields and all the submitted comments from being displayed at the bottom of the page the shortcode is used on?

    Many thanks in advance for your assistance.

    function faq_postsbycategory() {
    // the query
    $the_query = new WP_Query( array(
        'category_name' => 'faq',
        'posts_per_page' => 10,
    	'order' => 'ASC',
    	'orderby' => 'link'
    ) ); 
      
    // The Loop
    if ( $the_query->have_posts() ) {
        $string .= '<div class="postsbycategory widget_recent_entries">';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
                if ( has_post_thumbnail() ) {
                $string .= '<p>';
                $string .= '<b><a href="' . get_the_permalink() . '"rel="bookmark">' . get_the_post_thumbnail($post_id, array( 200, 100) ) . get_the_title() . '</a></b><br><h7>Posted: ' . get_the_date() . '</h7><br>' . get_the_excerpt() . '</p>';
                }
    		else {
                // if no featured image is found ADD EXCERPT CODING
                // $string .= '<p><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() . '</a><br><h7>Posted: ' . get_the_date() . '</h7><br>' . get_the_excerpt() . '</p>';
                }
                }
        } else {
        // no posts found
    }
    $string .= '</div>';
    
    return $string;
    
    /* Restore original Post Data */
    wp_reset_postdata();
    }
    // Add a shortcode
    add_shortcode('faq-posts', 'faq_postsbycategory');

    The page I need help with: [log in to see the link]

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator bcworkz

    (@bcworkz)

    That code is not responsible for the comments appearing. Comments are for the page on which the shortcode was placed. You can disable new comments for a specific page through the quick edit action link on the page listings table. You can disable new comments site wide in discussion settings.

    Existing comments will remain unless you delete them or create a commentless page template.

    Thread Starter clairedelune

    (@clairedelune)

    I don’t understand what you mean!? The comments are disabled on that page and there have never been any comments submitted on that page.

    The comments that are displaying are from the last post in the list generated by the shortcode. I know this for certain because I tested it by creating the comments on that post, not on the page where the shortcode is placed and yet they are displaying on the page.

    I do not understand what you are saying.

    • This reply was modified 3 years, 3 months ago by clairedelune.
    Thread Starter clairedelune

    (@clairedelune)

    Same thing is happening on my site home page.

    The home page has NEVER had any comments submitted there yet the comments from the last one of the posts in the short code generated list are being displayed.

    Thread Starter clairedelune

    (@clairedelune)

    I am going to do another experiment on another page that has comments disabled and nil comments ever submitted.

    This only started after the recent round of Plugin updates after the last core WP update.

    Thread Starter clairedelune

    (@clairedelune)

    Ok the experiment produced the same results:: comments on the posts displaying in the page where the shortcode is inserted.

    Logically this should not be happening so is it possibly a flaw in the theme?

    Be that as it may, for posterity …

    SOLUTION USING CSS

    .page-id-9999 #comments {
    display:none;
    }

    Replace the number 9999 in the page-id- to your own page. Find the page ID per the instructions here: 8 Proper Ways to Hide Elements on Your Website With CSS (2021)

    Happy coding.

    Moderator bcworkz

    (@bcworkz)

    That’s a reasonable solution, but the comments still exist and are just hidden from view. The fact they belong to the last post output by the shortcode is a good clue. The issue is the shortcode’s PHP has the wp_reset_postdata() call after the return $string; line. It never executes at that location. Relocate it to just above the return $string; line.

    Thread Starter clairedelune

    (@clairedelune)

    Being gracious: Thanks for that @bcworkz.

    NB: The comments are not hidden by the CSS, they are set to display:none which means exclusion from output.

    However, the result I am after is that they not be affected at all by the script so I will test your suggestion… Although from what you post it may be a case of leaving that line out all together if it is not necessary.

    Moderator bcworkz

    (@bcworkz)

    OK, not “hidden” in CSS terminology terms, but comments are still there in HTML source. Just not displayed to general users. It is still output to the page — check the HTML source for yourself if you need more convincing. PHP doesn’t know about CSS, it sends all of its output regardless of CSS rules.

    You need the reset line to execute to prevent comment output related to the final post in the loop. You still could in theory get comments related to the page itself unless page commenting is disabled.

    Thread Starter clairedelune

    (@clairedelune)

    That is interesting, thanks for the additional info. The CSS buffs would have us all believe something that is not true lol

    I’m working on an old iPad and unsure how to view source HTML … just another challenge for me to solve.

    I will test it out, I just haven’t had a chance yet.

    Thread Starter clairedelune

    (@clairedelune)

    I finally had the opportunity today to take another look at this and made the adjustment suggested by @bcworkz and it seems to be working correctly ~ thank you.

    Final code now looks like this with a couple of extra spaces and typo errors fixed:

    function faq_postsbycategory() {
    $the_query = new WP_Query( array(
        'category_name' => 'faq',
        'posts_per_page' => 10,
    	'order' => 'ASC',
    	'orderby' => 'link',
    ) );
    if ( $the_query->have_posts() ) {
        $string .= '<div class="postsbycategory widget_recent_entries">';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
                if ( has_post_thumbnail() ) {
                $string .= '<p>';
                $string .= '<b><a href="' . get_the_permalink() . '"rel="bookmark">' . get_the_post_thumbnail($post_id, array( 200, 100) ) . get_the_title() . '</a></b><br><h7>Posted: ' . get_the_date() . '</h7><br>' . get_the_excerpt() . '</p>';
                }
    	else {
                $string .= '<p><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() . '</a><br><h7>Posted: ' . get_the_date() . '</h7><br>' . get_the_excerpt() . '</p>';
                }
                }
        } else {
    }
    $string .= '</div>';
    wp_reset_postdata();
    return $string;
    }
    add_shortcode('faq-posts', 'faq_postsbycategory');
Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Custom Shortcode Function error: Listing Posts on a Page’ is closed to new replies.