WordPress.org

Support

Support » Plugins and Hacks » Display Posts Shortcode » [Resolved] Adding via PHP

[Resolved] Adding via PHP

  • Hi Bill,

    I was wondering if the short code would be recognized if it was added via a function added to the functions file and if I might be able to use a category function reference to have the short code pull the category it should display posts from based upon the category archive that it is being displayed on. I’m hoping to have a list of posts displayed on the top on every category archive (below the taxonomy description but before the loop) and I would love if I could have it automatically do this for each category with the addition of one singular function. Any thoughts?

    https://wordpress.org/plugins/display-posts-shortcode/

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Bill Erickson

    @billerickson

    In your theme file, you can wrap things in the function do_shortcode() to make it run shortcodes.

    Examples:

    <?php
    
    echo do_shortcode( '[display_posts'] );
    
    $my_output = '[display_posts]';
    echo apply_filters( 'do_shortcode', $my_output );

    Hi Bill,

    Thanks for the response. I am coming back to this finally. I’m trying to work out how I can automate it so that the post list will automatically be displayed at the top of each category archive listing the posts in that category. (Rather than me having to manually add it to the top of each category.) Here is what I have:

    function post_list_on_category_archive() {
    if ( is_category() )
        	echo '<div class="before-category">';
    	echo do_shortcode( '[display_posts category="$category_id"]' );
    
    	$my_output = '[display_posts category="$category_id"]';
    	echo apply_filters( 'do_shortcode', $my_output );
    	echo '</div>';
    
    };
    
    add_action('genesis_before_loop', 'post_list_on_category_archive');

    Does that look like it would work? Can the plugin understand how to get the category that is currently being displayed?

    Thank you so much!!

    Plugin Author Bill Erickson

    @billerickson

    A few issues I see:

    1. You haven’t defined $category_id. You’ll want $category_id = get_query_var( ‘cat’ ) in there.
    2. You’re displaying the shortcode twice. This might have been caused by confusion from my last post. I was showing you two different examples. One is using the do_shortcode() function, and the other uses the do_shortcode filter. You only need one of those, not both.

    If your trying to convert shortcode into php add this snippet into a theme file like footer.php (or whatever):

    <?php echo do_shortcode('[display-posts wrapper="div"]'); ?>

    source: http://codex.wordpress.org/Function_Reference/do_shortcode

    Thank you for the response, Bill, and thank you for the time. I know support is not really your thing.

    I am working though this and the only problem I seem to have is related to the current category generation. I am trying to use this code:

    //* Add List Above Category Archives
    function post_list_on_category_archive() {
    if ( is_category() ) {
        	echo '<div class="before-category">';
    	echo do_shortcode( '[display-posts posts_per_page="-1" order="ASC" orderby="title" columns="2" category="$category_id"]' );
    	$category_id = get_query_var( 'cat' );
    	echo '</div>';
    }
    
    };
    
    add_action('genesis_before_content', 'post_list_on_category_archive');

    If I remove the “category=…” the short code will display as expected. But for some reason adding in category=”$category_id” returns nothing where the shortcode should be.

    Thoughts?

    Plugin Author Bill Erickson

    @billerickson

    Try this:

    //* Add List Above Category Archives
    function post_list_on_category_archive() {
    if ( is_category() ) {
        	echo '<div class="before-category">';
    	$category_id = get_query_var( 'cat' );
    	echo do_shortcode( '[display-posts posts_per_page="-1" order="ASC" orderby="title" columns="2" category="' . $category_id . '"]' );
    	echo '</div>';
    }
    
    };
    
    add_action('genesis_before_content', 'post_list_on_category_archive');

    Still nothing. It is very odd that this one thing is causing it to disappear.

    The website, by the way, is http://enjoyrhinebeck.com/ if having that information tells you anything. Thanks.

    Plugin Author Bill Erickson

    @billerickson

    I’m sorry, but there must be an issue with your theme or plugins you are running. That code works for me.

    OK, thanks. I will keep trying to troubleshoot it. I’ll let you know if I can figure something out.

    I finally figured out the issue, and boy do I feel slow. It wasn’t working because the your shortcode can’t pull categories based upon the ID, only the slug.

    //* Add List Above Category Archives
    function post_list_on_category_archive() {
    if ( is_category() ) {
        	echo '<div class="before-category">';
    	$cat = get_query_var('cat');
      	$yourcat = get_category ($cat);
    	echo do_shortcode( '[display-posts posts_per_page="-1" order="ASC" orderby="title" columns="2" category="' . $yourcat->slug . '"]' );
    	echo '</div>';
    }
    
    };
    
    add_action('genesis_before_loop', 'post_list_on_category_archive', 40);

    With an adjustment to call the slug instead of teh category archive, it works perfectly. Thanks for all the help!!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘[Resolved] Adding via PHP’ is closed to new replies.
Skip to toolbar