Support » Fixing WordPress » Having a page only show posts from a specific category

  • I have a website, and it currently runs off of three different themes. Well I got the functionality working for it so that its a portfolio page, and then you can go to the blog, and get a blog layout as well.

    And I got it so that the blog only shows posts under the category blog….however, when you click on the entry, you go to the entry page for the blog, and it’ll have Next and Previous titles on the top of the blog….and those will let you go Next to hidden posts and posts I generally don’t want the public to see (client proofs and such)

    My website is located at http://zsuttonphoto.com. As you can see, its very portfolio based. But when you click blog (http://zsuttonphoto.com/blog/) it maintains the format, while making it vertical scroll, with blog entries.

    Well my theme is unique in the fact that my portfolios are also post entries, that just follow a different format for display.

    So if you go to an individual blog post, such at this — http://zsuttonphoto.com/actor-headshot-session-with-dorian-roberts/
    In the top left of the blog page, it says I can go back to Portrait Portfolio 1. I’d rather have it read it as though there are no other older entries, as I only want it to pull entries from the ‘Blog’ category…not all categories.

    I know, this is confusing, but I don’t really know of a better way to tell it. Does anyone know how to make it so the top of my blog entries page only ready from the Category ‘Blog’?

Viewing 15 replies - 1 through 15 (of 18 total)
  • Its worth noting that my individual entries are on a separate theme, so we can just hard wire it into the code without any issues elsewhere

    First, I doubt that your site is running under three different themes.

    Second, all public posts are viewable to all people. I would recommend you to set posts that you don’t want to show to normal users as private. The other option would be to remove the next or previous posts from the single.php file to prevent them from displaying at all.

    I assure you my website is running on three different themes. I designed them, afterall.

    But by setting them to private, aren’t I making it so no one can view them but me? The ultimate goal is to make proof galleries, that clients will be able to direct link to and select photos they’d like to have edited.

    And the general idea is to have the previous and next links on the top and bottom of individual posts, but making sure its only pulling entries from the category ‘Blog’

    There is no reason to run a website under many different themes (I don’t know if it is possible).

    You can certainly create a username and password with the ability to view private posts for your clients. You can also create them as pages instead of posts.

    There is plenty of reason if you understood the functionality of my website. And while, I could narrow it down to two different themes, its unlikely I could have it all embraced under one theme.

    And I’d be unable to create them as pages due to the functionality of the site. I’d prefer to just have it so that my blog only pulls entries under the category ‘Blog’. I know its possible, as I have that functionality within http://zsuttonphoto.com/Blog/. I’m hoping someone here would be able to provide me with that single line of code that would make that its standard function.

    You can change the previous and next links to display posts from the same category or exclude all other categories.

    Open your single.php that display the above post, then edit the next and previous php codes:

    http://codex.wordpress.org/Template_Tags/previous_post_link
    http://codex.wordpress.org/Template_Tags/next_post_link

    For example: <?php next_post_link(,,TRUE,); ?> to display next posts in the same category.

    My navigation is set up as an array, so I’m unable to just add a True statement for the category choice.

    if (have_posts()){
    					/* Display navigation to next/previous posts when applicable */
    					if (theme_get_option('theme_top_single_navigation')) {
    						theme_page_navigation(
    							array(
    							    'next_link' => theme_get_previous_post_link('&laquo; %link'),
    								'prev_link' => theme_get_next_post_link('%link &raquo;')
    							)
    						);

    You have a custom function to get the previous and next posts. Look for theme_page_navigation() in one of your theme files, probably in functions.php or other include ones.

    Or you can always make your own custom previous and next links:

    <div class="navigation">
    <div class="alignleft">
    << <?php previous_post_link(,,TRUE,); ?>
    </div>
    <div class="alignright">
    <?php next_post_link(,,TRUE,); ?> >></div>
    </div>

    Customize the the parameters for your needs.

    Here is the code in functions.php —

    if (!function_exists('theme_page_navigation')){
    	function theme_page_navigation($args = '') {
    		$args = wp_parse_args($args, array('wrap' => true, 'prev_link' => false, 'next_link' => false));
    		$prev_link = $args['prev_link'];
    		$next_link = $args['next_link'];
    		$wrap = $args['wrap'];
    		if (!$prev_link && !$next_link) {
    			if (function_exists('wp_page_numbers')) { // http://wordpress.org/extend/plugins/wp-page-numbers/
    				ob_start();
    				wp_page_numbers();
    				theme_post_wrapper(array('content' => ob_get_clean()));
    				return;
    			}
    			if (function_exists('wp_pagenavi')) { // http://wordpress.org/extend/plugins/wp-pagenavi/
    				ob_start();
    				wp_pagenavi();
    				theme_post_wrapper(array('content' => ob_get_clean()));
    				return;
    			}
    			//posts
    			$prev_link = get_previous_posts_link(__('Newer posts <span class="meta-nav">&rarr;</span>', THEME_NS));
    			$next_link = get_next_posts_link(__('<span class="meta-nav">&larr;</span> Older posts', THEME_NS));
    		}
    		$content = '';
    		if ($prev_link || $next_link) {
    
    			$content = <<<EOL
    	<div class="navigation">
    		<div class="alignleft">{$next_link}</div>
    		<div class="alignright">{$prev_link}</div>
    	 </div>

    And idea on what to change to make it category based? I’d appreciate any help you can give me.

    I would replace the code in the single.php:

    /* Display navigation to next/previous posts when applicable */
    if (theme_get_option('theme_top_single_navigation')) {
    	theme_page_navigation(
    	array(
    	'next_link' => theme_get_previous_post_link('&laquo; %link'),
    	'prev_link' => theme_get_next_post_link('%link &raquo;')
    	)
    	);
    }

    With:

    <div class="navigation">
    	<div class="alignleft">
    		<?php previous_post_link('%link','<< %title',TRUE,''); ?>
    	</div>
    	<div class="alignright">
    		<?php next_post_link('%link','%title  >>',TRUE,''); ?>
    	</div>
    </div>

    Am I able to embed html into that, as it does have a while script.

    <?php
    				if (have_posts()){
    					/* Display navigation to next/previous posts when applicable */
    					if (theme_get_option('theme_top_single_navigation')) {
    						theme_page_navigation(
    							array(
    								'next_link' => theme_get_previous_post_link('&laquo; %link'),
    								'prev_link' => theme_get_next_post_link('%link &raquo;')
    							)
    						);
    					}
    
    					while (have_posts())
    					{
    						the_post();
    						get_template_part('content', 'single');
    						comments_template();
    					}
    
    					/* Display navigation to next/previous posts when applicable */
    					if (theme_get_option('theme_bottom_single_navigation')) {
    						theme_page_navigation(
    							array(
    								'next_link' => theme_get_previous_post_link('&laquo; %link'),
    								'prev_link' => theme_get_next_post_link('%link &raquo;')
    							)
    						);
    					}
    
    				} else {    
    
    					theme_404_content();
    
    				}
    			?>

    Yes, you need to end the php code before the html and start it again after.

    <?php
    	if (have_posts()){ ?>
    
    		<div class="navigation">
    			<div class="alignleft">
    				<?php previous_post_link('%link','<< %title',TRUE,''); ?>
    			</div>
    			<div class="alignright">
    				<?php next_post_link('%link','%title  >>',TRUE,''); ?>
    			</div>
    		</div>
    
    		<?php
    		while (have_posts())
    		{
    			the_post();
    			get_template_part('content', 'single');
    			comments_template();
    		}
    		?>
    
    		<div class="navigation">
    			<div class="alignleft">
    				<?php previous_post_link('%link','<< %title',TRUE,''); ?>
    			</div>
    			<div class="alignright">
    				<?php next_post_link('%link','%title  >>',TRUE,''); ?>
    			</div>
    		</div>
    
    	<?php } else {    
    
    		theme_404_content();
    
    	}
    ?>

    Seems to work, thanks.

    However, it messes up the formatting pretty bad…Any idea on how to make the format unchanged while removing and posts not under the category ‘Blog’?

    Old Way – http://i.imgur.com/G4LOn.png

    New Way – http://i.imgur.com/AkYhd.png

    Sorry, I pasted you the code with the the starting and ending php. Just replace part from “if (have_posts()){ ?>” to “}” with the same part in single.php:

    if (have_posts()){ ?>
    
    		<div class="navigation">
    			<div class="alignleft">
    				<?php previous_post_link('%link','« %title',TRUE,''); ?>
    			</div>
    			<div class="alignright">
    				<?php next_post_link('%link','%title  »',TRUE,''); ?>
    			</div>
    		</div>
    
    		<?php
    		while (have_posts())
    		{
    			the_post();
    			get_template_part('content', 'single');
    			comments_template();
    		}
    		?>
    
    		<div class="navigation">
    			<div class="alignleft">
    				<?php previous_post_link('%link','<< %title',TRUE,''); ?>
    			</div>
    			<div class="alignright">
    				<?php next_post_link('%link','%title  >>',TRUE,''); ?>
    			</div>
    		</div>
    
    	<?php } else {    
    
    		theme_404_content();
    
    	}

    If it doesn’t work, paste your single.php file using http://pastebin.com/

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Having a page only show posts from a specific category’ is closed to new replies.