This is searchable archive of our old support forums, which operated from 2012 - 2016. To find out how to get support for your current theme, please visit our support page.

Tagged: 

Changing 'archive_no_posts' local based on post type

  • Creator
    Topic
  • #18490
    Eleven Sites
    Participant

    Hi Jason,

    I’m trying to customize the no post message for custom post types. But, I can’t get it to work. Can you point me in the right direction?

    /*——————————————————-*/
    /* Modify front end text strings
    /*——————————————————-*/
    function my_locals( $locals ) {
    if (get_post_type() == ‘photo’){
    $locals[‘archive_no_posts’] = ‘No photos available.’;
    }
    if (get_post_type() == ‘video’){
    $locals[‘archive_no_posts’] = ‘No videos available.’;
    }
    return $locals;
    }
    add_filter( ‘themeblvd_frontend_locals’, ‘my_locals’ );

Viewing 5 replies - 1 through 5 (of 5 total)
  • Author
    Replies
  • #18491
    Jason Bobich
    Keymaster

    The get_post_type() returns the post ID for a specific post, which you wouldn’t have if you’re displaying that message. In what context are the posts being displayed?

    #18498
    Eleven Sites
    Participant

    I’ve created custom queries in the functions file to be used in the post display shortcode like the following:

    <h2 class="assets">Photos</h2><?php
    echo do_shortcode('[post_grid query="photos" link="false" columns="4"]');
    ?>

    #18503
    Jason Bobich
    Keymaster

    And what context are these shortcodes being outputted? See, right now you’re just so focused on the specific post grid but I’m trying to figure out a WP conditional function you can use that would apply to the scenario where the shortcode exists.

    For example, if you were to use the shortcode in the content of a static page, then you could use is_page(‘whatever’) in your filter function.

    #18504
    Jason Bobich
    Keymaster

    By context, I mean how is WordPress displaying this code you’re writing? Is it in a template file? Which one?

    #18505
    Jason Bobich
    Keymaster

    In an instance where all of this was happening on the same page here’s another approach… In your template file or where ever this code is being outputted:

    <?php
    add_filter( 'themeblvd_frontend_locals', 'my_locals_to_photos' );
    echo '<h2 class="assets">Photos</h2>';
    echo do_shortcode('[post_grid query="photos" link="false" columns="4"]');
    remove_filter( 'themeblvd_frontend_locals', 'my_locals_to_photos' );
    
    add_filter( 'themeblvd_frontend_locals', 'my_locals_to_videos' );
    echo '<h2 class="assets">Videos</h2>';
    echo do_shortcode('[post_grid query="videos" link="false" columns="4"]');
    remove_filter( 'themeblvd_frontend_locals', 'my_locals_to_videos' );
    
    // ... and so on
    ?>

    Then, in your functions.php, you’d have these two functions setup:

    function my_locals_to_photos($locals) {
    	$locals['archive_no_posts'] = ‘No photos available.';
    	return $locals;
    }
    
    function my_locals_to_videos($locals) {
    	$locals['archive_no_posts'] = ‘No videos available.';
    	return $locals;
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.