I'm trying to have a post from a certain category display on my static homepage.
I seem to everything working just how I'd like with one exception.
I'd like the post to included the standard Continue reading (<!--more-->) link, but I can't seem to get it to work on my homepage instead all the post's content is displayed.
Here's the code I'm using to display the one post from a catagory on my home page:
<?php $recent = new WP_Query("cat=4&showposts=1"); while($recent->have_posts()) : $recent->the_post();?>
<h2><?php the_title(); ?></h2>
<?php the_content( __( 'Continue reading →')); ?>
<?php endwhile; ?>
How do I get the <!--more--> tag to work correctly with the above code?
The <!--more--> quicktag generally doesn't work on anything other than the Home page. Try following the advice here under "How to Read More in Pages", i.e. like this:
<?php
global $more;
$more = 0;
?>
//The code must be inserted ahead of the call to the content
Then continue as you were:
<?php $recent = new WP_Query("cat=4&showposts=1"); while($recent->have_posts()) : $recent->the_post();?>
<h2><?php the_title(); ?></h2>
<?php the_content( __( 'Continue reading →')); ?>
<?php endwhile; ?>
You'll need to set $more to 0 before every the_content() call;
It resets every time it hits the loop.
The discussion topic referenced from that Codex entry talks about exactly the problem you're solving, I think.
I was able to find some information here: http://codex.wordpress.org/Function_Reference/the_content
Found these lines which seem to do the trick:
global $more; // Declare global $more (before the loop).
$more = 0; // Set (inside the loop) to display content above the more tag.
the_content("Read More");
Added the information to my code and everything work! Added in the Read More link when the more tag was added to the post.
Here's my final code:
<?php $recent = new WP_Query("cat=4&showposts=1"); while($recent->have_posts()) : $recent->the_post();?>
<h2><?php the_title(); ?></h2>
<?php global $more;
$more = 0;
the_content("Read More");
?>
Related
I want display 30 posts on category.php , but after 10 post loop is terminating where as i have more then 30 posts for particular category . I have tried on category.php with if(have_posts() )
Ohh i got answer .. It is simply SETTINGS -> READING -> Blog pages show at most
#Dibya
try this way to show the number of posts on category.php without affecting blog page
<?php
$query = new WP_Query('category_name=Category&posts_per_page=30');
if($query->have_posts()) : while($query->have_posts()) : $query->the_post();
if (has_post_thumbnail()) {
?>
<?php the_post_thumbnail(); ?>
<?php
}
?>
<h2><?php the_title(); ?></h2>
<?php
the_excerpt(); // or the_content(); for full post content
endwhile;endif;
?>
This can also be resolved using posts_per_page = -1 parameter in wp_query. By using this , you don't need to bother about the backend settings. This will always works.
I am using the all-in-one event calendar plugin and it provides shortcodes for the user to enter into a post and then it will display the event information. The shortcode is [ai1ec events_limit="3"]. My Loop is below.
<?php
$eventArgs = array(
'category_name' => 'events_home'
);
$eventQuery = new WP_Query( $eventArgs );
if ($eventQuery->have_posts()) : while ($eventQuery->have_posts()) : $eventQuery->the_post();
?>
<?php the_content(); ?>
<?php
endwhile; endif; wp_reset_postdata();
?>
The output result only shows one event and then three dots "...", which I am assuming means the content the being shortened. Is there a way to NOT limit the_content(); I have tried:
<?php echo get_the_content(); ?>
But that only prints out the shortcode text.
Summary: Is there a way to NOT limit the_content();
Is your default setting shows content summary?
In this page from Wordpress Developer I've found this:
Overriding Archive/Single Page Behavior
If the_content() isn’t working
as you desire (displaying the entire story when you only want the
content above the Quicktag, for example) you can override
the behavior with global $more.
//If you need to display all of the content:
// Declare global $more (before the loop).
global $more;
// Set (inside the loop) to display all content, including text below more.
$more = 1;
the_content();
currently I am making a custom theme for my client and I am not a expert in this. My question is how to make different style for post from same category. Currently in my theme
Starting a New query for first post
<?php query_posts('showposts=1&cat=videos&offset=0'); if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
<div class="first-news">
<h2><a href="<?php the_permalink() ?><?php the_title(); ?></a></h2>
<?php if( has_post_thumbnail() ) { ?>
<?php the_post_thumbnail('video-thumb');?<?php} ?>
<?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,8); ?>
</div>
<?php endwhile; else: endif; ?>
then again starting the same query for remaining 4 posts with another div and style
<?php query_posts('showposts=4&cat=videos&offset=1'); if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
<div class="second-news">
<h3><a href="<?php the_permalink() ?><?php the_title(); ?></a></h3>
<?php if( has_post_thumbnail() ) { ?>
<?php the_post_thumbnail('news-thumb'); ?><?php } ?>
<?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,8); ?>
</div>
<?php endwhile; else: endif; ?>
this working perfectly, Is this correct? I think there may be a good solution which will query post only once and get the required number of posts from same category with different style.
What I want is on below image.
You should use the category template from wordpress.
Before loading your page, wordpress looks for the presence of specific templates, example from the page linked above.
1. category-slug.php
2. category-ID.php
3. category.php
4. archive.php
5. index.php
In order to activate “post formats” in WordPress 3.1+, you will need to open your theme’s functions.php file and paste the following code:
add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );
Note: aside, and gallery are not the only available post formats. The available list of post formats are:
aside – Typically styled blog format.
chat – A chat transcript.
gallery – A gallery of images.
link – A link to another site.
image – A single image.
quote – A quotation.
status – A short status update, usually limited to 140 characters. Similar to a Twitter status update.
video – A single video.
For the full list of post formats, refer to WordPress Codex.
Once you have added this code, you will see a new field in your post write panel in the right hand column where you see publish.
Upon writing the post, you can change the format and hit publish. This will allow you to display your post in a pre-styled format.
Edit your post loop.
Suppose in your case videos category post format is video
We are going to be utilizing the conditional tag: has_post_format()
if ( has_post_format( 'video' ) {
// Blog Category format
}
else
{
// Normal Formate
}
I hope this will help you. More Info...
I have the code below for a category in WordPress. It displays the name of the category when I want it to be displaying the title of the post. How do I get it to display the title of the post proper.
<? query_posts('category_name=implants');
?>
<h3><?php single_cat_title(); ?></h3>
<?php if (have_posts()) : while (have_posts()) :
the_post(); ?>
<?php the_content( __('Read the
rest of this page »', 'template')); ?>
<?php endwhile; endif; ?></p>
Do not use query_posts unless your intention is to modify the
default Wordpress Loop. Use WP_Query instead for standard Wordpress
queries.
Look at your code. You're calling single_cat_title(). It means
exactly what it looks like: You're pulling the title of the queried
category. You want to call the_title() to grab the post title.
Not as important as the above, but your opening tag is <? rather
than <?php. You should make it a habit of specifying your server-side language to avoid potential future problems, even though it might not be initially apparent.
Here's what your revised loop should look like:
<?php
$query = new WP_Query('category_name=implants');
if($query->have_posts()) : while($query->have_posts()) : $query->the_post();
?>
<h3><?php the_title(); ?></h3>
<?php
the_content( __('Read the rest of this page »', 'template'));
endwhile; endif;
wp_reset_postdata();
?>
I've built a custom page.php template. Very simple, essentially:
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php get_footer(); ?>
I've created a few pages, and if I visit their url, I just get the same page title.
I read up in the documentation, and it says to use the_title() and such only when in "the loop".
So presumably, I'm just being shown the first page in the "array".
Is there any way I get get the contents of a single page based on the url?
Edit: In fact, should I even need to do this? Refering to example templates, it looks like I'm doing everything right?
In my sidebar I was using a custom query.
This was called before trying to access the main page content, without resetting.
When doing a custom query you must reset after you've finished your loop like so:
$originalPost = $post;
$sidePosts = get_posts($queryArgs);
foreach($sidePosts as $post) {
setup_postdata($post);
// echo it out like a normal post.
}
$post = $originalPost;
or if you are using query_posts() (which you shouldn't in a sidebar):
wp_reset_query();
Which will take your post back to it's previous value.
For a custom page template please use the following to get everything correct
<?php
/* Template name: My custom template */
get_header();
if ( have_posts() ) while ( have_posts() ) : the_post();
the_title();
the_content()
endwhile;
get_sidebar();
get_footer();
?>