The use case is that I want to list the most recent posts on a page. Each post containing only the summary and title and a link to the full post.
I have created a template called Latest Posts and it loads ok. However I want get_content() to somehow understand that this template is an archive template and therefor only display the excerpt and not the full post.
I use the following "content" template for all other listings and would like to reuse it when listing my latest posts, but since it isn't either a search or an archive the first selection is skipped, and the_content() will show the whole post.
<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'bbl' ) ); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'bbl' ), 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
<?php endif; ?>
Any ideas? I've been looking around for ideas, and I've seen a lot of different ways to make archives, but I haven't been able to put it all together to fit my use case.
The solution is to use this code:
global $more; // Declare global $more (before the loop).
....
$more = 0;
....
More on this subject can be found in the documentation:
http://codex.wordpress.org/Function_Reference/the_content#Overriding_Archive.2FSingle_Page_Behavior
Now the_content() will not show anything after the more-tag
It's not really telling the engine that this is an archive page, but only mimic the behaviour
Related
The short problem: Editing the sanitize function has allowed me to add uppercase letters and symbols to my permalinks. However, when I use a symbol (like: ".", "=", "?") the link ends up in a 404 error. I'm guessing because the database can't access the page with a URL like that.
Are there any ways around this?
The long problem: I'm transitioning someone's site that was written in ASP on a windows server into a wordpress site. I've copied each page's content and created a wordpress site that duplicates the site structure of the current ASP one. But I need to keep the URL's the same, and his current site pages look like "http://thesite.com/ContentPage.aspx?page=subpage".
You can create a page hierarchy, apply a rewrite rule and create a page template, to solve the problem without having to add extra content to the permalink and the final URL would end up looking like the old ASP URL.
The query variable 'page' holds the pagenumber for a single paginated Post or Page, using this queryvar isn't recommended. You should review the need to maintain the url structure.
Taking this page structure for example:
Animals (permalink: http://thesite.com/animals/)
Lion (permalink: http://thesite.com/animals/lion)
Zebra (permalink: http://thesite.com/animals/zebra)
Others (permalink: http://thesite.com/animals/others)
Flowers (permalink: http://thesite.com/flowers/)
If I access http://thesite.com/Animals.aspx?page=Lion I should be presented the "Lion" page
For this to work you have to add to your functions file the add_rewrite_rule function with the following regex:
^([\w\d\-]*)(?:\.aspx?).*$
(I'm not used to regexp so i guess there's an easier way to write it.)
resulting in something like this:
add_rewrite_rule('^([\w\d\-]*)(?:\.aspx?).*$','index.php?pagename=$matches[1]','top');
Use it in a function like the example in the Codex. This way you'll access http://thesite.com/index.php?pagename=Animals when entering http://thesite.com/Animals.aspx?page=Lion in the address bar.
Do not forget to flush and regenerate the rewrite rules database after modifying rules.
So now you must get the 'page' queryvar and do a custom query to get the page children. The best way to do this is creating a page template. Create a file aspx-template.php in your theme folder and add the following:
<?php
/**
* Template Name: aspx Template
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
$pagename = get_post_field( 'post_name', get_the_ID() );
if ( isset($_GET['page']) ) : $pagename .= '/' . sanitize_text_field( $_GET['page'] ); endif;
$args = array(
'post_type' => 'page',
'pagename' => $pagename,
'post_status' => 'publish'
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
?>
<article>
<header class="entry-header">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
</div><!-- .entry-content -->
</article><!-- #post-## -->
<?php endwhile; wp_reset_postdata(); ?>
<?php else : ?>
<div>
No page found
</div>
<?php endif; ?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php get_footer(); ?>
Don't forget to associate the template to the parent page (Animals).
I need the option to build a page showing all posts of a specific category.
Showing all posts of a category can be done out-of-the-box by wordpress, I know. But I need the possibility to put some information about all those posts.
I know there's a plugin called "List category posts" (http://wordpress.org/plugins/list-category-posts/). It works but it's only showing the links to the posts. I need the full posts (like they are shown on the "blog page").
If you need to "do something" to results, look at
query_posts
via http://codex.wordpress.org/Function_Reference/query_posts
Here is a sketch that I think leans towards your needs using a custom loop. This can be inserted as needed via simple logic in your template:
// this sets up the filter parameters for a category id some_cat_id sorting asc
$args = array(
'cat' => $some_cat_id,
'order' => 'ASC'
);
// The query is applied via a conditional
if($some_conditional) { // for what ever reason we use the filter+args
query_posts( $args );
// this is also an opportunity to "do stuff" before the loop/output
}
// The Loop (simple example)
while ( have_posts() ) :
the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
As a long time WP user I avoid plugins at all costs in preference of writing sustainable code. Plugins are a point of failure and some of the biggest plugin factories out there are nothing but security issues wrapped in sugar.
Custom loops via conditionals using query "filtering" is amazing and this pattern can be extended to category, search, tags, and meta key:value pairs.
Additionally, by understanding the loop the formatting and output can be controlled in a manner that is easy to sustain. Some of the plugin logic is horrid and very inefficient, so always investigate any and all plugins when performance and security are important.
Here's what I find to be the most simple way to do this:
<?php query_posts('cat=25&showposts=3'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//You can change up the format below any way you'd like.
<li class="homeblock" style="max-width:400px;">
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div class="contentbox"><?php the_excerpt(); ?> </div>
</li>
<?php endwhile; endif; ?>
You can add this to a theme template file and all you need to change is the category id to the category you are trying get posts from. For example if your category id is '114' and you would like to show 9 posts it would look like the following:
<?php query_posts('cat=114&showposts=9'); ?>
If you need to add more info to the posts you should consider using custom fields to do that. Check out the plugin called Advanced Custom Fields.
Here is an example of a custom field being used in a loop:
<?php query_posts('cat=25&showposts=3'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li class="homeblock" style="max-width:400px;">
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div class="contentbox"><?php the_excerpt(); ?> </div>
<?php $article_link=get_post_meta($post->ID, 'article-link', true);?>
<?php if ( $article_link ) : ?>
<?php else : ?>
<?php endif; ?>
</li>
<?php endwhile; endif; ?>
In the above example, if the custom field 'article-link' has a value, then that value (a URL) is used as the href in a link instead of the permalink of the article.
Hope I have helped!
I would like to prevent the post's gallery from displaying when the post is listed on the homepage.
I'm thinking it will utilize add_filter and apply_filter when post in on homepage.
You can add a gallery to posts by clicking the add media button. You can select existing images or upload additional images that will create a gallery within the post. This embeds a shortcode in $post['content'] that looks like [gallery ids="37,38,39,40,41,42].
The issue is that by default it displays when the post is included on homepage as well as the individual post itself.
Update: This is what I am doing right now to achieve the requirement. I suspect there will be a more elegant way.
<div class="entry-content">
<!-- Begin Post Content -->
<?php if ( is_single() ) : ?>
<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'faceboard' ) ); ?>
<?php else : // Filter Gallery ShortCode out ?>
<?php
$content = '';
$content = get_the_content();
$content = preg_replace('/\[gallery\sids="[0-9]+(,[0-9]+)*,?"\s?(royalslider="\d")?\]/s',"",$content);
echo wpautop( $content, 1);
?>
<?php endif; // is_single() ?>
<!-- End Post Content -->
<?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'faceboard' ), 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
You can add the following to your theme's functions.php file or to a custom plugin (better, as you can disable it without touching the theme).
The filter post_gallery is used to create your own gallery, and the $content parameter comes empty by default. If it returns empty, the original [gallery] shortcode is processed.
Here, we are using a dummy empty value, so the filter is tricked into thinking that we are passing some actual gallery content, but it's just a white space.
add_filter( 'post_gallery', 'disable_home_galleries_so_17635042', 10, 2 );
function disable_home_galleries_so_17635042( $content, $atts )
{
// http://codex.wordpress.org/Conditional_Tags
if( is_home() )
return ' ';
return $content;
}
I want to only have the post title and meta (cat, tag, date) to be displayed on the results page.
I have used this code posted by Esmi at wp-forums to partially achieve this.
<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php elseif( !is_category() ) : ?>
<div class="entry-content">
<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentytwelve' ) ); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'twentytwelve' ), 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
<?php endif; ?>
This code in the child theme's content.php will produce Excerpts on the Search page results, but just the Title and Meta for the Category results.
I have tried adding !is_tag || !is_archive to the elseif statement.. then everything returns the full post.
I have replaced the elseif !is_category with elsif !is_tag and I once again get just the title and meta result on the tag page, which is what I want.
It seems I should be able to add this list to the else if statement to get what I want, but I must not the the correct code to do such.
I also tried replacing the top string php if is_search
with is_search || is_tag || is_archive || is_category
and the gave me whole posts as well.
This should be simple.. but I am stuck. Any help will be appreciated.
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...