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
Related
Below is my loop:
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post(); ?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php get_post_permalink(); ?>
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>
Get <?php get_post_permalink(); ?> should display the link yet this is what is being rendered. It is not displaying the permalink of the post
None of the other answers are correct. get_the_permalink() (you can use get_permalink() as well, since it's an alias) RETURNS the data, not ECHO. So, it will never be printed to the screen (most WP functions with get_ prefix work this way.)
You have two options:
Use get_permalink( get_the_ID() ) pass the current post id (if not in the loop) and echo it.
Use the_permalink() which will echo out the permalink (in the loop);
the_permalink():
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post(); ?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php the_permalink(); ?>
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>
get_permalink():
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post(); ?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php echo get_permalink(); ?>
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>
This will echo out the URL, but will not make the link clickable - you need to add it to an <a> tag:
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post(); ?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
Click Here
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>
If you want to get Post Permalink you should use get_permalink($post_id)
Wordpress get_permalink function Reference
Please try this:
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post();
$id = get_the_ID();
?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php get_the_permalink($id); ?>
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>
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 } ?>
Is there a way to combine content from a wordpress page's main textarea with the content in its custom template?
In this case I have a custom template that displays all posts from a single category, but I would also like to have a section that displays what is written in the wordpress admin Page area.
This is how I have the custom template set up to display relevant posts:
<?php query_posts('category_name=baby-coupons'); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ;?> <span class="post-date">- <?php the_time('F j, Y'); ?></span></h2>
<div class="row">
<div class="one-third">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
</div>
<div class="two-third last">
<?php the_excerpt() ;?>
</div>
</div><!--/row-->
<hr>
<?php endwhile; ?>
Above this I would like to have the wordpress pages admin area content display, what a user would normally write into the textarea to display on the page, is this possible?
This is what I've come up with:
<?php get_posts(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content() ;?>
<?php endwhile; ?>
<?php query_posts('category_name=baby-coupons'); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ;?> <span class="post-date">- <?php the_time('F j, Y'); ?></span></h2>
<div class="row">
<div class="one-third">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
</div>
<div class="two-third last">
<?php the_excerpt() ;?>
</div>
</div><!--/row-->
<hr>
<?php endwhile; ?>
Is this acceptable? It's working but I'm not sure how classy it is!
I have a few snippets of code where I want to check if the category is correct and if post actually exist, at the moment I have an if_category() function that then queries my required category but I think these are clashing so none of my posts for the category are being displayed, can anyone advise how I can fix this code?
PHP
<?php if(is_category('Rainwater Harvesting Product')) { ?>
<!-- Related Projects -->
<?php query_posts('category_name=rainwater-harvesting-project&showposts=5'); ?>
<?php if (have_posts()) { ?>
<div class="aside-mod related-projects">
<div class="curved-ctn">
<h2 class="module-header">Related Projects</h2>
<ul class="project-list clearfix">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li class="project">
<a href="<?php the_permalink();?>" class="project">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail');
} ?>
<h3><?php the_title(); ?></h3>
<!-- <i class="icon-project"></i> -->
</a>
</li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
</div>
</div>
<?php } ?>
<?php } ?>
<?php wp_reset_query(); ?>
It does not work because in your
<?php query_posts('category_name=Greywater Recycling Project&showposts=5'); ?>
you are giving to category_name, the Name instead of the category slug, the query_posts parameter, category_name takes only the slug. eg: it must be
<?php query_posts('category_name=greywater-recycling-project&showposts=5'); ?>
More info here: http://codex.wordpress.org/Function_Reference/query_posts
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>