Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Displaying title and image only for wordpress posts. WordPress

  • SOLVED

On my blog, harpoon71.com/blog, I want to display all posts under certain category, and sub-categories, as a title and featured image only. I don't want to use the 'more' feature, just title and image only.

In the code please assume the category is name 'camping' and it must recognise the sub-cats automatically..

I am using TwentyFifteen theme and have made a child theme.

Due to being a complete novice can you tell me where the code goes quite exactly.

Answers (5)

2017-03-27

Rempty answers:

Hello Brian
You need to copy paste the content template files to your child theme
content.php
content-link.php
content-page.php
content-search.php

After this you need to edit the files.


Brian McWeed comments:

Sorry, can't send you my details.

I have found the files you mentioned.

Now what?


Rempty comments:

replace your content.php with

<?php
/**
* The default template for displaying content
*
* Used for both single and index/archive/search.
*
* @package WordPress
* @subpackage Twenty_Fifteen
* @since Twenty Fifteen 1.0
*/
?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
// Post thumbnail.
twentyfifteen_post_thumbnail();
?>

<header class="entry-header">
<?php
if ( is_single() ) :
the_title( '<h1 class="entry-title">', '</h1>' );
else :
the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' );
endif;
?>
</header><!-- .entry-header -->
<?php
//Here use the id of the category
$category_id=123;
$categories=get_categories(
array( 'parent' => $category_id )
);
$array_cats=array();
foreach($categories as $cat){
$array_cats[]=$cat->term_id;
}
$array_cats[]=$category_id;
if(!has_category($array_cats)){
?>
<div class="entry-content">
<?php
/* translators: %s: Name of current post */
the_content( sprintf(
__( 'Continue reading %s', 'twentyfifteen' ),
the_title( '<span class="screen-reader-text">', '</span>', false )
) );

wp_link_pages( array(
'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentyfifteen' ) . '</span>',
'after' => '</div>',
'link_before' => '<span>',
'link_after' => '</span>',
'pagelink' => '<span class="screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>%',
'separator' => '<span class="screen-reader-text">, </span>',
) );
?>
</div><!-- .entry-content -->

<?php
// Author bio.
if ( is_single() && get_the_author_meta( 'description' ) ) :
get_template_part( 'author-bio' );
endif;
?>

<footer class="entry-footer">
<?php twentyfifteen_entry_meta(); ?>
<?php edit_post_link( __( 'Edit', 'twentyfifteen' ), '<span class="edit-link">', '</span>' ); ?>
</footer><!-- .entry-footer -->
<?php
}
?>

</article><!-- #post-## -->



Check for this line
//Here use the id of the category
$category_id=123;
and change 123 for the id of the category "camping"


Brian McWeed comments:

That code worked but it stripped away this box, see image.

Sorry, I know I said only title and image. Is it much work to add it?

Thanks.


Rempty comments:

Use this
<?php
/**
* The default template for displaying content
*
* Used for both single and index/archive/search.
*
* @package WordPress
* @subpackage Twenty_Fifteen
* @since Twenty Fifteen 1.0
*/
?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
// Post thumbnail.
twentyfifteen_post_thumbnail();
?>

<header class="entry-header">
<?php
if ( is_single() ) :
the_title( '<h1 class="entry-title">', '</h1>' );
else :
the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' );
endif;
?>
</header><!-- .entry-header -->
<?php
//Here use the id of the category
$category_id=123;
$categories=get_categories(
array( 'parent' => $category_id )
);
$array_cats=array();
foreach($categories as $cat){
$array_cats[]=$cat->term_id;
}
$array_cats[]=$category_id;
if(!has_category($array_cats)){
?>
<div class="entry-content">
<?php
/* translators: %s: Name of current post */
the_content( sprintf(
__( 'Continue reading %s', 'twentyfifteen' ),
the_title( '<span class="screen-reader-text">', '</span>', false )
) );

wp_link_pages( array(
'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentyfifteen' ) . '</span>',
'after' => '</div>',
'link_before' => '<span>',
'link_after' => '</span>',
'pagelink' => '<span class="screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>%',
'separator' => '<span class="screen-reader-text">, </span>',
) );
?>
</div><!-- .entry-content -->

<?php
// Author bio.
if ( is_single() && get_the_author_meta( 'description' ) ) :
get_template_part( 'author-bio' );
endif;
?>
<?php
}
?>
<footer class="entry-footer">
<?php twentyfifteen_entry_meta(); ?>
<?php edit_post_link( __( 'Edit', 'twentyfifteen' ), '<span class="edit-link">', '</span>' ); ?>
</footer><!-- .entry-footer -->


</article><!-- #post-## -->


Brian McWeed comments:

Thank you, that works but there is a problem with single post.

Click on a recent post in the sidebar, www.harpoon71.com , and the single post doesn't open, it should open full.


Cesar Contreras comments:

Is that what you really want to show? Or do you want to show your latest entries? You could try changing the Static Front Page in Appearance-> customize-> Static Front Page


Rempty comments:

FIxed added one condition to detect if is single page

<?php
/**
* The default template for displaying content
*
* Used for both single and index/archive/search.
*
* @package WordPress
* @subpackage Twenty_Fifteen
* @since Twenty Fifteen 1.0
*/
?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
// Post thumbnail.
twentyfifteen_post_thumbnail();
?>

<header class="entry-header">
<?php
if ( is_single() ) :
the_title( '<h1 class="entry-title">', '</h1>' );
else :
the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' );
endif;
?>
</header><!-- .entry-header -->
<?php
//Here use the id of the category
$category_id=123;
$categories=get_categories(
array( 'parent' => $category_id )
);
$array_cats=array();
foreach($categories as $cat){
$array_cats[]=$cat->term_id;
}
$array_cats[]=$category_id;
if(!has_category($array_cats) && !is_single()){
?>
<div class="entry-content">
<?php
/* translators: %s: Name of current post */
the_content( sprintf(
__( 'Continue reading %s', 'twentyfifteen' ),
the_title( '<span class="screen-reader-text">', '</span>', false )
) );

wp_link_pages( array(
'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentyfifteen' ) . '</span>',
'after' => '</div>',
'link_before' => '<span>',
'link_after' => '</span>',
'pagelink' => '<span class="screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>%',
'separator' => '<span class="screen-reader-text">, </span>',
) );
?>
</div><!-- .entry-content -->

<?php
// Author bio.
if ( is_single() && get_the_author_meta( 'description' ) ) :
get_template_part( 'author-bio' );
endif;
?>
<?php
}
?>
<footer class="entry-footer">
<?php twentyfifteen_entry_meta(); ?>
<?php edit_post_link( __( 'Edit', 'twentyfifteen' ), '<span class="edit-link">', '</span>' ); ?>
</footer><!-- .entry-footer -->


</article><!-- #post-## -->


Rempty comments:

Sorry my last answer was wrong
please use this instead
<?php
/**
* The default template for displaying content
*
* Used for both single and index/archive/search.
*
* @package WordPress
* @subpackage Twenty_Fifteen
* @since Twenty Fifteen 1.0
*/
?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
// Post thumbnail.
twentyfifteen_post_thumbnail();
?>

<header class="entry-header">
<?php
if ( is_single() ) :
the_title( '<h1 class="entry-title">', '</h1>' );
else :
the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' );
endif;
?>
</header><!-- .entry-header -->
<?php
//Here use the id of the category
$category_id=123;
$categories=get_categories(
array( 'parent' => $category_id )
);
$array_cats=array();
foreach($categories as $cat){
$array_cats[]=$cat->term_id;
}
$array_cats[]=$category_id;
if(!has_category($array_cats)){
?>
<div class="entry-content">
<?php
/* translators: %s: Name of current post */
the_content( sprintf(
__( 'Continue reading %s', 'twentyfifteen' ),
the_title( '<span class="screen-reader-text">', '</span>', false )
) );

wp_link_pages( array(
'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentyfifteen' ) . '</span>',
'after' => '</div>',
'link_before' => '<span>',
'link_after' => '</span>',
'pagelink' => '<span class="screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>%',
'separator' => '<span class="screen-reader-text">, </span>',
) );
?>
</div><!-- .entry-content -->

<?php
// Author bio.
if ( is_single() && get_the_author_meta( 'description' ) ) :
get_template_part( 'author-bio' );
endif;
?>
<?php
}
if(is_single() && has_category($array_cats)){
?>
<div class="entry-content">
<?php
/* translators: %s: Name of current post */
the_content( sprintf(
__( 'Continue reading %s', 'twentyfifteen' ),
the_title( '<span class="screen-reader-text">', '</span>', false )
) );

wp_link_pages( array(
'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentyfifteen' ) . '</span>',
'after' => '</div>',
'link_before' => '<span>',
'link_after' => '</span>',
'pagelink' => '<span class="screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>%',
'separator' => '<span class="screen-reader-text">, </span>',
) );
?>
</div><!-- .entry-content -->

<?php
// Author bio.
if ( is_single() && get_the_author_meta( 'description' ) ) :
get_template_part( 'author-bio' );
endif;
?>
<?php
}
?>
<footer class="entry-footer">
<?php twentyfifteen_entry_meta(); ?>
<?php edit_post_link( __( 'Edit', 'twentyfifteen' ), '<span class="edit-link">', '</span>' ); ?>
</footer><!-- .entry-footer -->


</article><!-- #post-## -->


Brian McWeed comments:

sorry, there still is a bug.

go to my sidebar and click on one of the 'recent posts'. it doesn't open a full post, in fact it doesn't open at all.

Keep up the good work, nearly there.


Rempty comments:

Sorry but your page donĀ“t load shows error Fatal error: Cannot redeclare twentyfifteen_widgets_init() (previously declared in /home1/harpoons/public_html/wp-content/themes/child-theme/functions.php:159) in /home1/harpoons/public_html/wp-content/themes/twentyfifteen/functions.php on line 168


Brian McWeed comments:

yeh, sorry, was testing some other code. Try now.


Rempty comments:

Use this code, i did some tests in my localhost

<?php
/**
* The default template for displaying content
*
* Used for both single and index/archive/search.
*
* @package WordPress
* @subpackage Twenty_Fifteen
* @since Twenty Fifteen 1.0
*/
?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
// Post thumbnail.
twentyfifteen_post_thumbnail();
?>

<header class="entry-header">
<?php
if ( is_single() ) :
the_title( '<h1 class="entry-title">', '</h1>' );
else :
the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' );
endif;
?>
</header><!-- .entry-header -->
<?php
//Here use the id of the category
$category_id=123;
$categories=get_categories(
array( 'parent' => $category_id )
);
$array_cats=array();
foreach($categories as $cat){
$array_cats[]=$cat->term_id;
}
$array_cats[]=$category_id;
if(!has_category($array_cats)){
?>
<div class="entry-content">
<?php
/* translators: %s: Name of current post */
the_content( sprintf(
__( 'Continue reading %s', 'twentyfifteen' ),
the_title( '<span class="screen-reader-text">', '</span>', false )
) );

wp_link_pages( array(
'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentyfifteen' ) . '</span>',
'after' => '</div>',
'link_before' => '<span>',
'link_after' => '</span>',
'pagelink' => '<span class="screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>%',
'separator' => '<span class="screen-reader-text">, </span>',
) );
?>
</div><!-- .entry-content -->

<?php
// Author bio.
if ( is_single() && get_the_author_meta( 'description' ) ) :
get_template_part( 'author-bio' );
endif;
?>
<?php
}
if(is_single() && has_category($array_cats)){
?>
<div class="entry-content">
<?php
/* translators: %s: Name of current post */
the_content( sprintf(
__( 'Continue reading %s', 'twentyfifteen' ),
the_title( '<span class="screen-reader-text">', '</span>', false )
) );

wp_link_pages( array(
'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentyfifteen' ) . '</span>',
'after' => '</div>',
'link_before' => '<span>',
'link_after' => '</span>',
'pagelink' => '<span class="screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>%',
'separator' => '<span class="screen-reader-text">, </span>',
) );
?>
</div><!-- .entry-content -->

<?php
// Author bio.
if ( is_single() && get_the_author_meta( 'description' ) ) :
get_template_part( 'author-bio' );
endif;
?>
<?php
}
?>
<footer class="entry-footer">
<?php twentyfifteen_entry_meta(); ?>
<?php edit_post_link( __( 'Edit', 'twentyfifteen' ), '<span class="edit-link">', '</span>' ); ?>
</footer><!-- .entry-footer -->


</article><!-- #post-## -->


//Dont forget to replace the ID of the category
//Here use the id of the category
$category_id=123;


Brian McWeed comments:

YES!!!

Well done, it worked.

I will now pay you.

Thanks for the help.

2017-03-27

Hai Bui answers:

Do you want to display all posts belong to "camping" category and its sub-categories on the main blog page? Or you mean display all posts on the category pages?


Brian McWeed comments:

Hi,

I want all posts belonging to 'camping' and its sub-cats to always be displayed as title and image only.

It doesn't matter if they are on the main blog page, a category listing, or an archive or search result, they must always be title and image.

Please ask more questions if you have any.

Thanks

2017-03-27

Andrea P answers:

Hello Brian,

it is not clear where you want to display those posts.. do you want to alter the default archives?
because if you want to display these posts into a sidebar, you could use my free plugin:
https://wordpress.org/plugins/wp-list-pages-by-custom-taxonomy/

you can download it from there, or just go to Plugins>Add Plugin in your admin panel, and search for "pages by custom taxonomy".

then you will have a widget in your section Appearance>Widgets, and you can drag&drop it into any sidebar.
then Select the category you want to display and you will have loads of other options to filter the posts and to display them as you wish (i.e. only thumbnail and title).


Andrea P comments:

instead of editing all the template files used to display the posts in each situation, maybe it's easier to just hide the content with a filter?
also because you should never edit the files of a main theme, you must create a child theme first, and then put your edits in there..
otherwise when you update the theme, the edits will be overwritten.

you should put this code filter into a child theme too, but at least is just a single function that you can easily put back after you updated..
so, if you put this into the theme's functions.php file, it will hide the content of any post which is within the "camping" category or any of its children cat.
the content will be hidden everywhere the post is displayed.

add_filter( 'the_content', 'bwd_hide_content' );
add_filter( 'the_excerpt', 'bwd_hide_content' );

function bwd_hide_content($content){

$cat_slug = 'camping';
$cat = get_term_by( 'slug', $cat_slug, 'category');
$cat_id = $cat->term_id;
$children = get_terms( array( 'taxonomy' => 'category', 'parent' => $cat_id, 'fields' => 'ids' ) );

$post_id = get_the_ID();
$cats = wp_get_post_categories( $post_id, array( "fields" => "ids" ) );

foreach ( $cats as $category ){

if ( $cat_id == $category || in_array( $category, $children ) ){
$content = "";
break;
}
}

return $content;

}


Brian McWeed comments:

That is a good answer. I am trying one other option then I will give you a go.

Thank you


Andrea P comments:

ok! :)

this version of the code is even better :

add_filter( 'the_content', 'bwd_hide_content' );
add_filter( 'the_excerpt', 'bwd_hide_content' );

function bwd_hide_content($content){

$cat_slug = 'camping';
$cat = get_term_by( 'slug', $cat_slug, 'category');
$cat_id = $cat->term_id;
$children = get_terms( array( 'taxonomy' => 'category', 'parent' => $cat_id, 'fields' => 'ids' ) );

$post_id = get_the_ID();

if ( has_category( $cat_id, $post_id ) || has_category( $children, $post_id ) ){
$content = "";
}

return $content;

}


Brian McWeed comments:

Either I entered your code wrong, I just copied and pasted it straight under the function php code, or it doesn't work.

I got a blank screen and an error message.


Andrea P comments:

what's the error message?


Andrea P comments:

I've tested it in my site and it works..

this version is even slimmer, as it doesn't retrieve the post_id cause I've realised that the has_Category already defaults to the current post.

add_filter( 'the_content', 'bwd_hide_content' );
add_filter( 'the_excerpt', 'bwd_hide_content' );

function bwd_hide_content($content){

$cat_slug = 'camping';
$cat = get_term_by( 'slug', $cat_slug, 'category');
$cat_id = $cat->term_id;
$children = get_terms( array( 'taxonomy' => 'category', 'parent' => $cat_id, 'fields' => 'ids' ) );

if ( has_category( $cat_id ) || has_category( $children ) ){
$content = "";
}

return $content;

}


Brian McWeed comments:

the screen went white and it gave a php error message, said something about a certain line. sorry I'm not technical.


Andrea P comments:

try the latest version I've posted.
and copy and paste here the error in case you get it again


Andrea P comments:

just thinking..
if in your functions.php code at the end you have this:

?>

make sure you put my code BEFORE that line


Brian McWeed comments:

I just pasted into the standard Twenty Fifteen theme function file, right on the bottom, there was no ?>

Now with the site broken here is the error message -

Fatal error: Cannot redeclare twentyfifteen_widgets_init() (previously declared in /home1/harpoons/public_html/wp-content/themes/child-theme/functions.php:159) in /home1/harpoons/public_html/wp-content/themes/twentyfifteen/functions.php on line 168


Andrea P comments:

it looks like you do have a child-theme.
and if you have created one, then the functions.php file (as well as the style.css) shouldn't be copied from the parent theme, otherwise you get duplicated functions and it crashes.

if you need to create the functions.php in your child, just create an empty file functions.php in your child theme.

start with:

<?php

and then paste my code

no need to close ?> at the end, so just paste my code below the opening <?php and it will work

works in my test site ;)

2017-03-27

Marko Nikolic answers:

Hey,

If you want to show different layout for posts that belong to specific category (and subcategories) on Blog page and other archive pages (e.g. "The Church Inn, Mobberley, Knutsford" to show featured photo and title only if in specific category, but others to keep content), you would have to edit content.php file (and upload it to child theme). Inside that file, you would have to use conditional is_category - https://developer.wordpress.org/reference/functions/is_category/

I am testing it on my site atm, so will try to send you final code soon :)


Brian McWeed comments:

Thank you, that is a good answer, looking forward to your reply.


Cesar Contreras comments:

try this:

<?php
/**
* The default template for displaying content
*
* Used for both single and index/archive/search.
*
* @package WordPress
* @subpackage Twenty_Fifteen
* @since Twenty Fifteen 1.0
*/
?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
// Post thumbnail.
twentyfifteen_post_thumbnail();
?>

<header class="entry-header">
<?php
if ( is_single() ) :
the_title( '<h1 class="entry-title">', '</h1>' );
else :
the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' );
endif;
?>
</header><!-- .entry-header -->

<div class="entry-content">
<?php
//Here use the id of the category
$category_id=123;
$categories=get_categories(
array( 'parent' => $category_id )
);
$array_cats=array();
foreach($categories as $cat){
$array_cats[]=$cat->term_id;
}
$array_cats[]=$category_id;
if(!has_category($array_cats)):
/* translators: %s: Name of current post */
the_content( sprintf(
__( 'Continue reading %s', 'twentyfifteen' ),
the_title( '<span class="screen-reader-text">', '</span>', false )
) );

wp_link_pages( array(
'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentyfifteen' ) . '</span>',
'after' => '</div>',
'link_before' => '<span>',
'link_after' => '</span>',
'pagelink' => '<span class="screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>%',
'separator' => '<span class="screen-reader-text">, </span>',
) );
endif;
?>
</div><!-- .entry-content -->

<?php
// Author bio.
if ( is_single() && get_the_author_meta( 'description' ) ) :
get_template_part( 'author-bio' );
endif;
?>

<footer class="entry-footer">
<?php twentyfifteen_entry_meta(); ?>
<?php edit_post_link( __( 'Edit', 'twentyfifteen' ), '<span class="edit-link">', '</span>' ); ?>
</footer><!-- .entry-footer -->

</article><!-- #post-## -->

Check for this line
//Here use the id of the category
$category_id=123;
and change 123 for the id of the category "camping"

2017-03-27

Cesar Contreras answers:

<?php
$args = array(
'category_name' => 'camping',
'posts_per_page' => -1
);

$myquery = new WP_Query( $args );

get_header(); ?>

<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">

<?php while ($myquery->have_posts()) : $myquery->the_post(); ?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
// Post thumbnail.
twentyfifteen_post_thumbnail();
?>

<header class="entry-header">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
</header><!-- .entry-header -->

<div class="entry-content">
<?php //the_content(); ?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentyfifteen' ) . '</span>',
'after' => '</div>',
'link_before' => '<span>',
'link_after' => '</span>',
'pagelink' => '<span class="screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>%',
'separator' => '<span class="screen-reader-text">, </span>',
) );
?>
</div><!-- .entry-content -->

<?php
// Author bio.
if ( is_single() && get_the_author_meta( 'description' ) ) :
get_template_part( 'author-bio' );
endif;
?>

<footer class="entry-footer">
<?php twentyfifteen_entry_meta(); ?>
<?php edit_post_link( __( 'Edit', 'twentyfifteen' ), '<span class="edit-link">', '</span>' ); ?>
</footer><!-- .entry-footer -->

</article><!-- #post-## -->

<?php endwhile; ?>

</main><!-- .site-main -->
</div><!-- .content-area -->

<?php

wp_reset_postdata();

get_footer();
?>