• Resolved travismallen

    (@travismallen)


    Hi All,

    I had an idea that if I create a category per page with the same name I could dynamically filter the loop for each page by category, without having to create a separate loop for each page.

    My results are not working as I hoped, it returns all of the pages and categories.

    Has anyone done this before? Below is the URL to the site and the function…Thanks for the help!

    http://www.maggieflaniganstudio.com/mfs/our-studio/

    <?php function my_index_loop() { ?>
    	<?php if (is_home()) : ?>
    	<!--Custom stuff for homepage--!>
    	<?php else : ?>
    	<?php
    	$title = the_title();
    	$page_name = str_replace(" ", "-", $title);
    	$pageslug = $post->post_name; // name of the page (slug)
    	$catslug = get_category_by_slug($pageslug); // get category of the same slug
    	$catid = $catslug->term_id;  // get id of this category
    	$this_query= 'cat=' . $catid. '';
    	?>
    	<?php $other_query = new WP_Query($this_query);?>
    		<?php while ($other_query->have_posts()): $other_query->the_post(); ?>
    			<div class="middle-block">
    				<?php
    					the_title();
    					the_content('Read more...');
    				?>
    			</div>
    			<div class="bottom-block">
    				<div class="quote">
    					<h3>"Craft is there to sustain the original impulse, and to preserve the momentum"
    					<br />-Stanley Kunitz</h3>
    				</div>
    			</div>
    		<?php endwhile; ?>
    	<?php endif;?>
    <?php }
    add_action('thematic_indexloop', 'my_index_loop');
Viewing 10 replies - 1 through 10 (of 10 total)
  • I read a thread recently for doing just that, using a page’s name to pull posts from a category with the same name. It was an older thread, but the code used is still valid.

    Here’s 2 links for similar requests.
    http://wordpress.org/support/topic/318531?replies=3
    http://wordpress.org/support/topic/319812?replies=3

    Or there are additional results you may want to look through, i only had a quick look.
    http://wordpress.org/search/page+title+category?forums=1

    None of those are the thread i read before, but the above should still be relevant in helping.. 🙂

    vtxyzzy has done some stuff you might find usefuL

    http://wordpress.org/support/topic/349582

    But one category per page is somewhat ‘duplicative’ of the natural Category archive process that WordPress provides…

    But one category per page is somewhat ‘duplicative’ of the natural Category archive process that WordPress provides…

    I’d been thinking the same thing, but it seems to be a fairly popular request nonetheless, the whole “Show my category on a page” thing..

    Thread Starter travismallen

    (@travismallen)

    Thanks! I’ve been putting most of my code in functions.php vs. a separate template. I’ll see if I can merge some of the above with my approach and post results. Thanks again!

    P.S. I agree that is somewhat duplicative – I’m using this approach because the majority of the content is not blog oriented, so the categories function as more of a data binding tool than a tagging/group attribute.

    Thread Starter travismallen

    (@travismallen)

    Turns out if was a simple tweak.

    For anyone’s future reference. my goal was you dynamically change the loop to filter by category name where it was equal to the page name.

    <?php
    	$title = wp_title('', FALSE);
    	$page_name = str_replace(" ", "-", $title);
    	$this_query= 'category_name=' . $page_name;
    	?>
    	<?php $other_query = new WP_Query($this_query);?>
    		<?php while ($other_query->have_posts()): $other_query->the_post(); ?>
    			<div class="middle-block">
    				<div class="general-block">
    					<?php the_content('Read more...'); ?>
    				</div>
    			</div>
    			<div class="bottom-block">
    				<div class="quote">
    					<h3>"Craft is there to sustain the original impulse, and to preserve the momentum"
    					<br />-Stanley Kunitz</h3>
    				</div>
    			</div>
    		<?php endwhile; ?>

    I know i’ve seen a great example for doing just that, provided by one of the regular members, i still can’t find the thread though (not that there’s anything wrong with the suggestions already).

    I’ve added a few more tags to the thread, hopefully it will help future users looking for information.

    And thanks for posting back your solution… 🙂

    Thread Starter travismallen

    (@travismallen)

    No problem!

    Side note: You might want to trim the $title string to be on the safe side. I.e. $page_name = str_replace(" ", "-", trim($title));

    That’s all I got!

    This code helped me – thanks. I did change it slightly as I wanted to show posts where the tag was equal to the title…

    <?php
    		$title = wp_title('', FALSE);
    		$page_name = str_replace(" ", "-", trim($title));
    		$this_query= 'tag=' . $page_name;
    		?>
    		<?php $other_query = new WP_Query($this_query);?>
    
    			<?php while ($other_query->have_posts()): $other_query->the_post(); ?>
    <!-- stuff -->
    <?php endwhile; ?>

    Nice

    And how can I do if I’d like to loop posts with a LIKE filter on Title or on Description? Thanks

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Dynamically filtering loop where page name equals category’ is closed to new replies.