0

I have a custom post type called press.

I'm on the template single-press.php

I want to get the post titles of my custom post type named as "press".

Here is my code,please guide me?

    <?php function all_posts_custom_posts( $query ) {
            $post_type =  $query->query_vars['post_type'];

            if ( 'press' == $post_type ){
                    $query->query_vars['posts_per_page'] = -1;
                    return;
            }
    } 
    add_action('pre_get_posts', 'all_posts_custom_posts',1); ?>

And also highlight the current post from the list by adding a class to it.

  • Be extremely careful with ['posts_per_page'] = -1; consider moving to a variable and calling apply filters on it to ensure that should you wish to change this to all recent posts, or to allow plugin / theme users to customise via hooks instead of code editing theme or plugin. – MrMesees Dec 15 '16 at 5:26
1

As you are on, 'single-press.php' , this is single 'press' post type page. As per your question, it seems you want to display archive of all 'press' posts, along with current posts highlighted.

As we are on single 'press' post page, no need to check for post type.

We can directly add following loop with modification in structure, as per your need,

global $post;
$post_args = array(
        'post_type' => 'press',
        'post_status' => 'publish'
        );
$press_posts = get_posts($post_args);   
if(!empty($press_posts)){
foreach($pres_posts as $single_post){
    ?>
    <a href="<?php echo get_permalink($post_id); ?>" class="<?php if($single_post->ID == $post->ID ) echo 'wdm-current-post'; ?>"><?php echo $single_post->post_title; ?></a><?php
}
}

So now we have given a class 'wdm-current-post' to the current post. All the posts will now be listed with the current post highlighted in the list.

  • foreach line press_posts is missing a s – Rich Dec 19 '19 at 21:16
0

#You can use this WP_Query

$arg = new WP_Query( array( 'post_type' => 'postypename') ); 

while ( $arg->have_posts() ) : $arg->the_post();

   the_title(); 


endwhile;
0

You can get all posts of custom post type as you required to get all posts title with its permalink by using wp-query.

Code where you want to display all posts title with its permalink

As I am going to get custom posts title in list

  <ul class="dropdown-menu scrollable-menu" aria-labelledby="dropdownspecialist">
      <?php 
       global $loop;
       $all_posts = array( 'post_type' => 'press', 'posts_per_page' => -1 );
       $loop = new WP_Query( $all_posts );
        if($loop->have_posts()){
        while($loop->have_posts()){
            $loop->the_post();
        ?>
        <li><a href="<?php the_permalink(); ?>" class="cat_link"><?php the_title(); ?></a></li>
        <?php
        }
        }
        wp_reset_query();
        ?>
  </ul>

Second you ask to highlight the active link from the list, you can do this with some jQuery

 jQuery(document).ready(function(){
   var current = location.pathname;
    jQuery('.cat_link').each(function(){
    var jQuerythis = jQuery(this);
    // if the current path is like this link, make it active
    // indexOf(something) if the value never occurs it returns -1 and in this condition if indexOf(something) !== -1 means result !== -1 or results exists then add class active.
    if(jQuerythis.attr('href').indexOf(current) !== -1){
        jQuerythis.addClass('active');
    }
  });
});

That's it.

-1

What you describe is called a Post Type Archive and it comes baked in with WordPress. It should show up on your site at example.com/press, assuming that you haven't give it a different slug with the rewrite arg of on your register_post_type() function for the post type.

At that address, the query is automatically going to be showing all posts of the type "press." However, if you want to show all of them without paging, you will need to use pre_get_posts:

function wpse162905_modify_query( $query ) {
    if( !is_admin() && is_main_query() && is_post_type_archive( 'press' ) ) {
        $query->set( 'posts_per_page', -1 );
    }
}
add_action( 'pre_get_posts', 'wpse162905_modify_query' );

Finally, you'll want to modify the page template to show just the title as a link. To do that, you'll modify the archive-press.php theme file. As shown on the Template Hierarchy codex page, you can see that is the template that controls post type archives. At its most basic, that file probably includes this:

<?php if( have_posts() ) : ?>
    <ul>
        <?php while( have_posts() ) : the_post(); ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></li>
        <?php endwhile; ?>
    </ul>
<?php endif; ?>
  • Hi mrwweb, thanks for your answer. I used your code but it only shows the link and title of the current post, not all posts of press – wpuser Sep 29 '14 at 16:18
  • Make sure you're on the post type archive page. You can find that with get_post_type_archive_link( 'press' );. The Post Type Archive Link plugin for adding the archives to menus is handy too. – mrwweb Sep 29 '14 at 19:09
  • Also, make sure that you have more than one press post. Even if the pre_get_posts isn't working, you should see the default 10 posts per page. – mrwweb Sep 29 '14 at 20:40
  • By stating "And also highlight the current post from the list by adding a class to it." it's clear he doesn't want the standard post type archive – Jeffrey von Grumbkow Apr 25 '17 at 8:16
  • The OP appears to want a list of titles for that post type on the single post view/template – jdm2112 Jul 27 '18 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.