• Resolved rocke

    (@shashibhushan)


    Hi i want to show **only Standard FORMAT POST** IN MY CUSTOM TAXONOMY PAGE , WHICH IS AS :

    **Actually i have many category and format in my project , hence on this page i need only to show STANDARD FORMAT AND specific category**

    <?php
        /**
         * Locations taxonomy archive
         */
        get_header();
        $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
        ?>
        <div class="wrapper">
        	<div class="primary-content">
        		<h1 class="archive-title"><?php echo apply_filters( 'the_title', $term->name ); ?> News</h1>
    
        		<?php if ( !empty( $term->description ) ): ?>
        		<div class="archive-description">
        			<?php echo esc_html($term->description); ?>
        		</div>
        		<?php endif; ?>
    
        		<?php if ( have_posts() ): while ( have_posts() ): the_post(); ?>
    
        		<div id="post-<?php the_ID(); ?>" <?php post_class('post clearfix'); ?>>
        			<h2 class="post-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
        			<div class="content clearfix">
        				<div class="post-info">
        					<p><?php the_time(get_option('date_format')); ?> by <?php the_author_posts_link(); ?></p>
        				</div><!--// end .post-info -->
        				<div class="entry">
        					<?php the_content( __('Full story…') ); ?>
        				</div>
        			</div>
        		</div><!--// end #post-XX -->
    
        		<?php endwhile; ?>
    
        		<div class="navigation clearfix">
        			<div class="alignleft"><?php next_posts_link('« Previous Entries') ?></div>
        			<div class="alignright"><?php previous_posts_link('Next Entries »') ?></div>
        		</div>
    
        		<?php else: ?>
    
        		<h2 class="post-title">No News in <?php echo apply_filters( 'the_title', $term->name ); ?></h2>
        		<div class="content clearfix">
        			<div class="entry">
        				<p>It seems there isn't anything happening in <strong><?php echo apply_filters( 'the_title', $term->name ); ?></strong> right now. Check back later, something is bound to happen soon.</p>
        			</div>
        		</div>
    
        		<?php endif; ?>
        	</div><!--// end .primary-content -->
    
        	<div class="secondary-content">
        		<?php get_sidebar(); ?>
        	</div><!--// end .secondary-content -->
    
        <?php get_footer(); ?>
Viewing 15 replies - 1 through 15 (of 15 total)
  • skim-

    (@skim-)

    A simple way to accomplish this without a new WP Query :

    https://codex.wordpress.org/Function_Reference/get_post_format

    Here I’ve modified two of your lines of code. Note that get_post_format returns false if Standard ( i.e. no ) post format is set.

    <?php if ( have_posts() ): while ( have_posts() ): the_post(); if( !get_post_format() ) : ?>
    
    // do all of your display
    
    <?php endif; endwhile; ?>
    Thread Starter rocke

    (@shashibhushan)

    @ Skim,

    Thanks buddy its work … bro can you help me like this way ? :

    I am using Custom Taxonomy, hence in my taxonomy page i am seeing all post with all category ,

    But my category also include Photo and Video , which i don’t want to show in above loop , also i have given Format to Photo and Video ( as gallery , video rep. )… hence my logic was if my post format is gallery or video then its related post will not be seen in above loop code , which with above code is solved , but is there any other solution, i means to say : excluding that photo and video category id from above loop … hence all category post will be seen in above loop excluding photo and video category id ….

    Note : i am not using any custom post type … just using custom taxonomy …

    Thread Starter rocke

    (@shashibhushan)

    After some research help :

    I changed my code

    <?php if ( have_posts() ): while ( have_posts() ): the_post(); ?>

    With

    <?php $query = new WP_Query( 'cat=-1,-2 ); ?>
    
     <?php if ( $query->have_posts() ): while ( $query->have_posts() ): $query->the_post(); ?>

    But my all page shows same posts… actually i have a link some thing like this : test.com/motion/aamir , test.com/motion/salman , test.com/motion/ex … where “motion” is my custom taxonomy and amir,salman ,ex are slug …. now after click on aamir i can only see aamir post excluding photos,video post … similarly for salman and others after using above your code … all page showing aamir post only .. but photos and video category are exclude –

    Thread Starter rocke

    (@shashibhushan)

    correct : ” now after click on aamir i can only see aamir post excluding photos,video post … similarly for salman and others after using above your code … all page showing aamir post only .. but photos and video category are exclude – ”

    To

    now after click on aamir i can only see aamir post excluding photos,video post … But if i click on salman link or any other link .. then also its showing aamir post only .. its means

    <?php $query = new WP_Query( 'cat=-1,-2 ); ?>
    
     <?php if ( $query->have_posts() ): while ( $query->have_posts() ): $query->the_post(); ?>

    after using above code all links have aamir post only.

    but photos and video category are exclude –

    what i do that cat are exclude + each link have its own posts as per taxonomy slug

    Thread Starter rocke

    (@shashibhushan)

    Note : i am not using any custom post type … just using custom taxonomy …

    Any one .. need it badly .. pls help me guyss

    Moderator bcworkz

    (@bcworkz)

    When you create a new WP_Query, it means any arguments resulting from the link clicked on are ignored. The query is made solely on the arguments you provide and the default values for arguments not provided.

    In order to modify the query and still include arguments from the link, you need to use the ‘pre_get_posts‘ action and set the query vars you want to alter. When you set query vars this way, cat=-1,-2 is improper. That argument gets translated into a ‘category__not_in’ array, so when hooking the ‘pre_get_posts’ action you should use $query->set('category__not_in', array( 1, 2,));

    Thread Starter rocke

    (@shashibhushan)

    Can you please help me how i can do the same in above code… as this is my own custom taxonomy hence , how i can use ‘pre_get_posts’ action under function .php , i have tried below one : but its not working dear :

    function my_custom_exclude_cat( $query ) {
        if ( !is_home()  ) {
            $query->set('category__not_in', array( 1, 2));
        }
    }
    add_action( 'pre_get_posts', 'my_custom_exclude_cat' );

    if i use above action then ID are excluded but all slug link ( motion/aamir , motion/salman ) showing aamir post only , also if you remb in my previous Topic( http://wordpress.org/support/topic/post-page-own-page-theme?replies=9 )i had given an reference website link example which is same as my template lookalike , hence if i used above then my Photo and Video section ( which is another wp query loop) goes Blank .

    even tried as per document link :

    function my_custom_exclude_cat( $query ) {
    if (!is_home() ) {
    $query->set('cat', '-1,-2');
    }
    }
    add_action( 'pre_get_posts', 'my_custom_exclude_cat' );

    if i use above action then in all pages this cat id is exclude .
    as i only want to exclude above category id from my custom taxonomy page ( taxonomy-motion.php ) for particular area ( section or only its WHILE LOOP ) as below that i have again call another while ( wp _ query loop ) for Photo and Video section for resp slug… reference : http://www.missmalini.com/aamir-khan-2/

    Thread Starter rocke

    (@shashibhushan)

    hey , i was doing some mistake ,

    function my_custom_exclude_cat( $query ) {
        if ( !is_home()  ) {
            $query->set('category__not_in', array( 1, 2));
        }
    }
    add_action( 'pre_get_posts', 'my_custom_exclude_cat' );

    Above action is working , but is excluding cat(photo : 1, Video : 2) from all .php page, i don’t want …. i want to exclude cat(1,2) only from my custom taxonomy (taxonomy-motion.php) page and that to for 1,2 loop in which its BIO and Latest Posts of resp. SLUG is coming ..

    As in my custom taxonomy (taxonomy-motion.php) page i have 4 Section ( 4 different Loop )

    1 } Is bio Details of resp. Slug ( aamir,salman,etc..) with exclude cat(1,2) ,
    2 } Top Latest Posts of resp. Slug ( aamir,salman,etc..) with exclude cat(1,2) ,
    3 } All Latest Photos ( cat : 1 )Only of resp. Slug ( aamir,salman,etc..),
    4 } All Latest Video (cat: 2)Only of resp. Slug ( aamir,salman,etc..).

    reference : http://www.missmalini.com/aamir-khan-2/

    hence if i use above action then 1 , 2 Loop is working well . But my 3,4 is saying Not Details , as we have exclude cat(1,2).

    So i want to exclude cat(1,2) only from 1,2 loop of my custom taxonomy page

    Thread Starter rocke

    (@shashibhushan)

    Also like to add :

    cat(1,2,3) where 1-photo,2-video,3-bio.

    About above 4 loop :


    1} this loop is
    Bio of slug excluding cat(1,2)is used as :

    $bio = new WP_Query();
    $bio->query("cat=3&tag='<?php echo apply_filters( 'the_title', $term->slug ); ?>'"); ?>
      <?php  while ($bio->have_posts()) : $bio->the_post(); ?>
       // do stuff;
    <?php endwhile; ?>

    2 } In Second Loop which is getting all post excluding cat(1,2) don’t have WP_QUERY();

    <?php  if ( have_posts() ): while ( have_posts() ): the_post();   ?>
    <?php  endwhile; ?>

    3} All Latest Photos ( cat : 1 )Only of resp. Slug ( aamir,salman,etc..),

    $photo = new WP_Query();
    $photo ->query("cat=1&tag='<?php echo apply_filters( 'the_title', $term->slug ); ?>'"); ?>
      <?php  while ($photo ->have_posts()) : $photo ->the_post(); ?>
       // do stuff;
    <?php endwhile; ?>

    4} All Latest Videos( cat : 2 )Only of resp. Slug ( aamir,salman,etc..),

    $video= new WP_Query();
    $video->query("cat=2&tag='<?php echo apply_filters( 'the_title', $term->slug ); ?>'"); ?>
      <?php  while ($video->have_posts()) : $video->the_post(); ?>
       // do stuff;
    <?php endwhile; ?>

    this is how i am using all above loop in my custom taxonomy(taxonomy-motion.php) page.


    pre_gets_post

    function my_custom_exclude_cat( $query ) {
        if ( !is_home()  ) {
            $query->set('category__not_in', array( 1, 2));
        }
    }
    add_action( 'pre_get_posts', 'my_custom_exclude_cat' );

    Hence how i can use above pre_get_posts action.. so that my 2 loop do-not show cat(1,2) post , but my 3,4 loop doesn’t get effect … and my all loop work properly for resp Slug ( as mention : motion/aamir , motion/salman ..etc — where motion is custom taxonomy , also note i don’t have any custom post type )

    Thanks wait for best

    Moderator bcworkz

    (@bcworkz)

    As you have discovered, anything you set through ‘pre_get_posts’ is applied everywhere. Thus you typically will need to use conditional statements to insure the query vars are only set when it is appropriate. A good function to use in a conditional statement is is_main_query(). This will distinguish the main query run from URL parameters from any of your $query = new WP_Query( $args ); loops.

    To insure the restrictions are only applied to pages for your motion taxonomy, you can check the existing query vars for that condition, and only set the category restrictions when that is the case. I don’t have access to my usual references right now, so this example is likely flawed, but something like this in your ‘pre_get_posts’ call back should do the job:

    if( is_main_query() && 'motion' == $query->get('tax')){
       //set your desired category restrictions here
    }

    Thread Starter rocke

    (@shashibhushan)

    Hey bcworkz,

    First of all i am deeply thanksful for your kind support till now 🙂

    Dude i have try below one and its seem to work properly till now ,

    function my_custom_exclude_cat( $query ) {
    if ($query->is_tax( 'motion') && $query->is_main_query()) {
            $query->set('category__not_in', array( 1, 2));
        }
    }
    add_action( 'pre_get_posts', 'my_custom_exclude_cat' );

    what you say its perfect or should i used :

    function my_custom_exclude_cat( $query ) {
    if ($query->is_tax( 'motion') && $query->is_main_query()) {
            $query->set('category__not_in', array( 1, 2));
        }
    }
    add_action( 'pre_get_posts', 'my_custom_exclude_cat' );

    OR

    function my_custom_exclude_cat( $query ) {
    if( is_main_query() && 'motion' == $query->get('tax') || $query->is_tax( 'motion')  ){
            $query->set('category__not_in', array( 1, 2));
        }
    }
    add_action( 'pre_get_posts', 'my_custom_exclude_cat' );

    Pls let me know.

    Once again Thanks 🙂

    Moderator bcworkz

    (@bcworkz)

    I’m pleased I’m able to help you!

    The following is an important concept to understand. Please ask for clarification if you still do not fully understand, I don’t mind 🙂 Also, I still don’t currently have access to my usual references so I can’t verify some of the details here, I’ll have access again in a couple days.

    is_tax() is a WP function. You call it without any preface, $query->is_tax() is improper. is_tax() alone is all that’s needed. Same goes for is_main_query(). $query in this context (passed to your callback for ‘pre_get_posts’) is an object of the WP_Query class. Classes have certain methods like (for WP_Query) get() and set(). Other classes have other methods, but maybe not get() and set(). Class methods are called like this: $query->get(). Functions like is_tax() are not class methods, which is why $query->is_tax() is improper.

    Now… is_tax() is a special type of function (still not a class method) – it is a “template tag”. This means it typically can only be called inside the “Loop”. This is because many template tags expect certain global values like $post to be properly defined. Such globals are not properly defined outside of the loop. (is_main_query() will work inside of ‘pre_get_posts’ callbacks)

    That’s the important part. The rest is useful too, but if you don’t understand the rest it’s not a big deal.

    Here’s where things get fuzzy because I cannot verify through testing. is_tax() in particular is not totally dependent on the loop, it is established somewhere in the query parsing process. It may or may not be defined when pre_get_posts fires. It’s easy enough to test using normal debug methods. I don’t recall which one to use though 🙁 Usually, simply echoing something is adequate. However, doing so from certain filters and actions doesn’t work because the output is discarded by the normal WP output process, so you cannot see the results of your debugging efforts. There are other ways to get debug information, but they are more involved. IIRC, echo is adequate for ‘pre_get_posts’. Thus a simple test to see if is_tax() will work or not in your ‘pre_get_posts’ callback:

    if( is_main_query() $$ is_tax('motion')) {
       echo '<pre>Motion taxonomy detected</pre>';
    } else {
       echo '<pre>Motion taxonomy NOT detected</pre>';
    }

    Load your motion archive page, one of the messages should appear below your header IIRC, or an error will occur, or you will see no output at all. If the taxonomy detected message appears, then you can use the function, otherwise you must use $query->get().

    $query->get() will always work for you, but I’m unsure exactly what query var you need to get. I believe it is ‘tax’ but I’m not sure. What you can do is put if ( is_main_query()) {echo '<pre>'; var_dump($query); echo '</pre>'; } in your callback.

    (I always try to wrap debug output in <pre> tags so the output shows up on the page better.) Request the motion archive page. Search through the output for ‘motion’. Whatever tag is associated with ‘motion’ is the query var you want to get. It may ‘tax’ or ‘taxonomy’ or I don’t know what. There will be something or you requested the wrong page.

    Sorry for the long post, I would normally do some of this testing for you. It’s faster to do it than describe it! As it happens, I’m unable to do this testing myself right now. Anyway, it’s good experience for you 🙂 I hope you can make sense of all this. If not, I’ll give you a more concise answer in a couple days. I’ll check back before then, but I’ll still be handicapped. Good luck!

    Thread Starter rocke

    (@shashibhushan)

    Hey bcworkz,

    Dude thanks once again for your great help , and also for your great knowledge sharing .

    As you said i tried below for test in my function.php ( for pre_get_posts ):

    if( is_main_query() $$ is_tax('motion')) {
       echo '<pre>Motion taxonomy detected</pre>';
    } else {
       echo '<pre>Motion taxonomy NOT detected</pre>';
    }

    And i got message ‘Motion taxonomy detected’ on my custom taxonomy ( motion ) page.

    Also i query:

    if( is_main_query()){echo var_dump($query);}

    i have test with “pre” tag itself .. actually it was not showing in post properly hence removed 🙂

    and search for motion and i got :

    public ‘taxonomy’ => string ‘motion’

    let me know .
    thanks 🙂

    Moderator bcworkz

    (@bcworkz)

    That’s a little odd about the <pre> tag, but no matter, you obviously figured it out 🙂

    That’s good news about is_tax() working! I guess my concern was ill founded. Since the little test you did for me was successful, you can go ahead and safely use the first script from your post before last where you say “i have try below one and its seem to work properly”. Sorry for my false alarm, but I wanted to be sure all was proper and couldn’t do it myself.

    Now we also know "motion" == $query->get('taxonomy) would work as well, though there is no need since is_tax() works. It’s a little confusing that we get ‘taxonomy’ from the query, yet we request (without pretty permalinks) index.php?tax=motion and check with is_tax(). Which is why I thought we needed to get ‘tax’ instead of ‘taxonomy’. Your testing proved me wrong. Sigh.

    Thread Starter rocke

    (@shashibhushan)

    Hey bcworkz,

    Dude you itself have share such massive knowledge about tax, it was about testing for which you didn’t have some permission .. ok whatever .. thanks once again for your kind support till end.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘How to show only Standard Format post in my custom taxonomy page wordpress 3.8.1’ is closed to new replies.