show empty message on empty taxonomy wordpress - php

im working on wordpress theme and everything goes fine but i have this problem
i make a php page "taxonomy.php" it shows custom taxonomy posts
the problem i have if i visit empty taxonomy it shows me a posts from other taxonomy insted of showing error message
the code i use to display:
<?php
get_header();
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=9&post_type=covers'.'&paged='.$paged);
$term = $wp_query->queried_object;
?>
<div id="primary" class="site-content span12">
<div id="content" class="entry-content" role="main">
<?php
$product_terms = wp_get_object_terms($post->ID, 'cover_category');
if(!empty($product_terms)){
if(!is_wp_error( $product_terms )){
foreach($product_terms as $term){
echo '<h4 style="color:#cb2027">'.$term->name.'</h4>';
//Category Desciprion
echo '<h6 style="color:#cb2027">'.term_description($term->term_id, 'cover_category').'</h6>';
}
}
}
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<h5><?php the_title(); ?></h5>
<?php endwhile; ?>
<?php get_footer(); ?>
Now if i visit this
http://localhost/wp/?cover_category=as -> this taxonomy as have 6 posts and show them correct
if i visit this http://localhost/wp/?cover_category=wawa -> this taxonomy wawa have 0 posts and it show posts from as taxonomy

This is an example for default category page:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div style="height: 190px;">
<?php if ( has_post_thumbnail()) : ?>
<div class="thumb" style="float: left; width:170px;">
<?php the_post_thumbnail('thumbnail'); ?>
</div>
<?php endif; ?>
<div style="float:left; width:475px;">
<h1><?php the_title(); ?></h1>
<?php the_excerpt(); ?>
En savoir plus
</div>
</div>
<?php endwhile; ?>
<?php else: ?>
<h3>Page en cours de construction</h3>
<?php endif; ?>

Related

Cannot get post_content in customize page?

I've created a customize page show all posts with template:
<?php
/*
Template Name: All posts
*/
get_header();
?>
</header>
<div role="main" id="content" class="content-warp">
<div class="container">
<div id="primary" class="content-area col-md-8 post-list">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<div class="entry">
<?php the_content(); ?>
<?php
$current_date = "";
$count_posts = wp_count_posts();
$nextpost = 0;
$published_posts = $count_posts->publish;
$myposts = get_posts(array('posts_per_page' => $published_posts));
foreach ($myposts as $post) :
get_template_part('content', 'content-single');
?>
<?php endforeach; ?>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php get_sidebar('page'); ?>
<div style="clear:both"></div>
</div>
</div>
<?php get_footer(); ?>
But it not show post_content of Posts (all remain data are normal).
By the way, with default UI Categories (content.php), i just call below code and everything Ok: the same UI with new template but have post_content).
<?php get_template_part( 'content', 'single' ); ?>
I don't know why post_content is null in new my template. I'm using Llorix One LiteVersion: 0.1.7
Any help, thanks.
I try read document again and realize the_content not set data yet.
So, just add setup post data, look like:
...
foreach($myposts as $post) : setup_postdata( $post );
...
Also You can use new WP Query
$myposts = new WP_QUery(array(
'posts_per_page' => $published_posts
));
if ($myposts->have_posts()): ?>
<?php while ($myposts->have_posts()) {
$myposts->the_post();
get_template_part('content', 'content-single');
} ?>
<?php endif ?>
<?php wp_reset_query(); ?>

I want to display sub category post only in sub category but not in parent category

<?php get_header(); ?>
<section id="container" class="<?php echo tempera_get_layout_class(); ?>">
<div id="content" role="main">
<?php if ( have_posts() ) : ?>
This is the code to display sub category in a category page with images
<?php $this_category = get_category($cat); ?>
<ul style="line-height: 5.7em;">
<?php $id = get_query_var('cat');
$args = array('parent' => $id );
$catdesc = $cat->category_description;
foreach (get_categories($args) as $cat):?>
<a href="<?php echo get_category_link($cat->term_id); ?>">
<img style= "width:250px; height:150px; padding-left:20px; margin-top:20px;" src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
<div style="display:inline-block;position:absolute;margin-left:-150px;margin-top:150px;"><?php echo ("$cat->cat_name"); ?></div></a>
<?php endforeach ?>
</ul>
Here the code ends for displaying sub category
<?php
$category_description = category_description();
if ( ! empty( $category_description ) )
echo apply_filters( 'category_archive_meta', '<div class="category- archive-meta">' . $category_description . '</div>' );
?>
</header>
<?php while ( have_posts() ) : the_post(); ?>
<?php
get_template_part( 'content/content', get_post_format() );
?>
<?php endwhile; ?>
<?php if($tempera_pagination=="Enable") tempera_pagination(); else tempera_content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Nothing Found', 'tempera' ); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'tempera' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php endif; ?>
<?php cryout_after_content_hook(); ?>
</div><!-- #content -->
<?php tempera_get_sidebar(); ?>
</section><!-- #primary -->
<?php get_footer(); ?>
The problem is that all the posts in the category display under the category and the subcategory.
But i want to display posts under that subcategory only, not in category.
So please any one give me suggestions
Please use the below code. It will help you.
$category = '{enter your category name here}';
$categoryID = get_cat_ID($category);
$subcategories = get_categories('child_of=' . $categoryID);
foreach($subcategories as $subcategory) {
$subcategory_posts = get_posts('cat=' . $subcategory->cat_ID);
foreach($subcategory_posts as $subcategory_post) {
$postID = $subcat_post->ID;
echo get_the_title($postID);
}
}

How to don't show post having a specified tag in my homepage?

I am pretty new in WordPress development and I am trying to implement this custom theme that handle the so called featured posts: http://lnx.asper-eritrea.com/
As you can see in the posts area of the homepage I have the Articoli in evidenza sub area that contains my featured posts and under it the Ultimi Articoli subarea that contains the latest posts.
To implment this I use the posts tag and in the futured posts area I show the posts having the tag=featured condition.
So this is my code:
<section id="blog-posts">
<header class="header-sezione">
<h2>Articoli in evidenza</h2>
</header>
<?php query_posts('tag=featured');?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="featured-posts">
<h1><?php the_title(); ?></h1>
<div class="meta">
Scritto da <span class="author"><?php the_author_link(); ?></span> // <?php the_category(', ') ?> // <?php comments_popup_link('Nessun Commento', '1 Commento ', '% Commenti'); ?>
</div>
<div class="featured-details"><?php the_excerpt()?>
<?php $featured_img = get_post_meta($post->ID, 'featured_img', $single = true); ?>
<img src="<?php echo $featured_img ?>" alt="<?php the_title(); ?>" />
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
<header class="header-sezione">
<h2>Ultimi Articoli</h2>
</header>
<?php
if (have_posts()) :
// Start the Loop.
while (have_posts()) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part('content', get_post_format());
endwhile;
else :
// If no content, include the "No posts found" template.
get_template_part('content', 'none');
endif;
?>
</section>
As you can see first I show the posts having a tag featured by the use of query-posts() function:
<?php query_posts('tag=featured');?>
Now my problem is that if a post have the featured tag I don't want that it is shown in the latest post area (at this time it is shown). So I tried to use this code:
<header class="header-sezione">
<h2>Ultimi Articoli NOT FEATURED</h2>
</header>
<?php query_posts('tag != featured');?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="featured-posts">
<h1><?php the_title(); ?></h1>
<div class="meta">
Scritto da <span class="author"><?php the_author_link(); ?></span> // <?php the_category(', ') ?> // <?php comments_popup_link('Nessun Commento', '1 Commento ', '% Commenti'); ?>
</div>
<div class="featured-details"><?php the_excerpt()?>
<?php $featured_img = get_post_meta($post->ID, 'featured_img', $single = true); ?>
<img src="<?php echo $featured_img ?>" alt="<?php the_title(); ?>" />
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
But don't work and the featured post still shown in the homepage. As you can se I tried so specify that, to be shown, a post can't have the featured tag:
<?php query_posts('tag != featured');?>
Why don't work? What am I missing? Can you help me to fix this issue?
Tnx
To return posts that do not contain a particular tag, you should use pass the ID of the term into the tag__not_in argument.
// get the term using the slug and the tag taxonomy
$term = get_term_by( 'slug', 'featured', 'post_tag' );
// pass the term_id to tag__not_in
query_posts( array( 'tag__not_in' => array ( $term->term_id ) );
is_front_page()
You should try <?php is_front_page(); ?> or vice versa.
http://codex.wordpress.org/Function_Reference/is_front_page
<?php if (is_front_page()) { ?>
<!-- Do something -->
<?php } else { ?>
<!-- Add else only if you need it! -->
<?php } ?>

Wordpress: Check posts have comments allowed

I have a page that displays certain posts from a certain category, in this case category 33 (Tutorials) and it currently outputs the title, post excerpt and permalink to the posts in this category:
<?php $top_query = new WP_Query('cat=33'); ?>
<?php while($top_query->have_posts()) : $top_query->the_post(); ?>
How Can I specify that the posts returned should only be ones that have comments enabled?. I have tried wrapping it in:
<?php if(comments_open()) : ?>
Hover that needs to be used within the loop :(
Thanks in advance
try this one
<?php if( have_posts() ): ?>
<?php while( have_posts() ): the_post();?>
<?php if(comments_open()){ ?>
<div class="news-row">
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<div class="newsimagebox">
<?php //$feat_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID),'thumbnail');
$images = the_post_thumbnail();?>
<?php echo $images;?>
</div>
<?php endif; ?>
<div class="news-content">
<h5><?php the_title(); ?></h5>
<p><?php the_excerpt();?></p>
<div class="readmore">Read More</div>
</div>
</div>
<?php } ?>
<?php endwhile;?>
<?php endif; //wp_reset_query(); ?>
Thanks

Call top posts in different divs

I want to have my latest post show up with all info(title, author, thumbnail, and content) but only the title of the second most recent post in another div. Here is my code. It renders the divs correctly but the 'title' in the second div is the 'title' of the latest post still.
<div id="blog-pane">
<div id="blog-post">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="post-title">
<?php the_title(); ?>
</div>
<div id="post-author">
<?php the_author(); ?>
<?php the_date(); ?>
<?php the_time(); ?>
</div>
<div id="post-image">
<?php the_post_thumbnail(); ?>
</div>
<div id="post-text">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php rewind_posts(); ?>
</div>
<div id="post-link-1">
<?php
query_posts( 'p' );
while ( have_posts() ) : the_post();
the_title();
endwhile;
wp_reset_query();
?>
</div>
</div>
</div>
<?php get_footer(); ?>
you can try to introduce some skip logic to have it skip the first post,
<div id="post-link-1">
<?php
query_posts( 'p' );
$count = 0;
while ( have_posts() ) {
if ($count++ == 0) {
the_post();
continue;
}
the_post();
the_title();
}
wp_reset_query();
?>
</div>

Categories