starkbr
starkbr

Reputation: 229

Custom Post Query WordPress and a better way to display posts

This seems a little noob, but I didn't found a better option. I created a custom loop to display only the title of custom post type I created.

Example:

Custom Post Type: Atuação

The problem is: I can't "validate" at the menu if the post is active or only a link. Example: My visitor is visiting the Direito Penal Empresarial page. But the menu don't display any class so I can customize it. It just shows the <a href> link.

See the code of the custom loop below.

<ul class="menu-advogados">
    <?php
        // WP_Query arguments
        $args = array (
            'post_type'              => 'atuacao_posts',
            'pagination'             => false,
            'order'                  => 'ASC',
            'orderby'                => 'title',
        );

        // The Query
        $exibir_atuacao_posts = new WP_Query( $args );

        // The Loop
        if ( $exibir_atuacao_posts->have_posts() ) {
            while ( $exibir_atuacao_posts->have_posts() ) {
                $exibir_atuacao_posts->the_post();
        ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php 
            }
        } else {
            echo "Nenhum post encontrado";
        }

        // Restore original Post Data
        wp_reset_postdata();
        ?>
    </ul>

There is any better solution for this? Or if not, how can I add the "active" class to the href?

UPDATE: You can check out the website live.

Upvotes: 0

Views: 39

Answers (2)

jay.jivani
jay.jivani

Reputation: 1574

use this

// WP_Query arguments
$args = array (
    'post_type'              => 'atuacao_posts',
    'post_status'            => 'publish',
    'pagination'             => false,
    'order'                  => 'ASC',
    'orderby'                => 'title',
);

// The Query
$query = new WP_Query( $args );

<?php if ( $query ->have_posts() ) : ?>

    <!-- the loop -->
    <?php while ( $query ->have_posts() ) : $query ->the_post(); ?>
        <h2><?php the_title(); ?></h2>
    <?php endwhile; ?>
    <!-- end of the loop -->

    <?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

for WP_Query consider this link
it's great article...

Upvotes: 0

user2839914
user2839914

Reputation: 56

You need to store current post ID in a variable then you need to compare current Post ID with list item Post ID if both are same then apply active class. So your code will be something like this-

<ul class="menu-advogados">
    <?php
         global $post;
         $post_id = $post->ID; // Store current page ID in a variable.

        // WP_Query arguments
        $args = array (
            'post_type'              => 'atuacao_posts',
            'pagination'             => false,
            'order'                  => 'ASC',
            'orderby'                => 'title',
        );

        // The Query
        $exibir_atuacao_posts = new WP_Query( $args );

        // The Loop
        if ( $exibir_atuacao_posts->have_posts() ) {
            while ( $exibir_atuacao_posts->have_posts() ) {
                $exibir_atuacao_posts->the_post();
        ?>
        <li><a href="<?php the_permalink(); ?>" <?php echo ($post_id==$post->ID)?'class="active"':''; ?> ><?php the_title(); ?></a></li>
        <?php 
            }
        } else {
            echo "Nenhum post encontrado";
        }

        // Restore original Post Data
        wp_reset_postdata();
        ?>
    </ul>

Upvotes: 1

Related Questions