• I would like to create shortcuts to specific pages on my site using a page slug, not an ID, which is how the below code works. Can anyone help? I posted about this months ago, but I got no action.

    Here’s a screenshot of what I’m after that just uses static HTML…

    https://cl.ly/image/141O2z0W0A11

    To be clear, the below code works fine. I just don’t want to use an ID as the live site has a certain set of IDs and then locally I have a different set. And they could possibly change again, so even if I did get them in sync somehow, I’d much rather use a slug. HUGE THANKS to anyone that can help me here. This is a really useful bit of code for anyone that wants something similar on their homepage.

    <?php
    						$args=array(
    						'orderby' =>'parent',
    						'order' =>'asc',
    						'post_type' =>'page',
    						'post__in' => array(2,10,9,7),
    						);
    						$page_query = new WP_Query($args);
    					?>
    
    					<?php while ($page_query->have_posts()) : $page_query->the_post(); ?>
    					   <div class="teaser">
    						<h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
    						<?php the_excerpt(); ?>
    					    </div>
    					<?php endwhile; ?>
Viewing 15 replies - 1 through 15 (of 15 total)
  • Hakkim

    (@hakkimpahammed)

    Try this code. Example below

    $the_slug = 'my-slug';
    $args=array(
    	'orderby' =>'parent',
            'name' => $the_slug,
    	'order' =>'asc',
    	'post_type' =>'page',
    	);
    Thread Starter specialmachine

    (@specialmachine)

    Yes, wonderful – I can do it with one slug, but I’m after multiple slugs. Perhaps I’m just not doing that part right as this code breaks the page…

    <?php
    					$the_slug = 'food', 'cocktails', 'wine';
    					$args=array(
    						'orderby' =>'parent',
    					        'name' => $the_slug,
    						'order' =>'asc',
    						'post_type' =>'page',
    						);
    					$page_query = new WP_Query($args);
    				?>
    
    				<?php while ($page_query->have_posts()) : $page_query->the_post(); ?>
    					<div class="teaser">
    					<h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
    					<?php the_excerpt(); ?>
    					</div>
    				<?php endwhile; ?>
    Hakkim

    (@hakkimpahammed)

    Yes, it allows only one slug at a time, so you have to run this code multiple times to get the result you want and that’s not a good way of coding. But I wonder,why didn’t you try to set some specific tag or custom fields for those posts you want and then try to retrieve them by the same tag or custom field? so that there would be lot of options available.

    Thread Starter specialmachine

    (@specialmachine)

    Hi Hakkim – I really don’t know how to do any of this. I’m not a developer. I know what I’m after, and then when it comes to how to do it, I just scour the web looking for a snippet of code.

    Hakkim

    (@hakkimpahammed)

    For every post/page you would get an option from admin side to set a tag or custom field in its edit page. You can use multiple tags or custom fields for a single post. Later you can use the same snippet of code by changing the arguments values.
    For ex:

    $args=array(
    	'orderby' =>'parent',
    	'tag' => 'apples+oranges',
    	'order' =>'asc',
    	'post_type' =>'page',
    	);
     query_posts($args);

    so that you can fetch all posts having the specified tags. Note that multiple tags are separated by ‘+’ sign.

    Thread Starter specialmachine

    (@specialmachine)

    Okay, I enabled tagging for pages by grabbing some code here and slapping it in my functions.php. I tagged each page with the corresponding name, so just like the slug names. Then I tried to add in your code with the updates to the tag names and it broke the page. Again, lack of understanding for how this works. Sorry. This is the code i’m trying to use now…

    <?php
    	$args=array(
    		'orderby' =>'parent',
    		'tag' => 'food+cocktails+wine',
    		'order' =>'asc',
    		'post_type' =>'page',
    		);
    	 query_posts($args);
    ?>
    
    <?php while ($page_query->have_posts()) : $page_query->the_post(); ?>
    	<div class="teaser">
    	<h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
    	<?php the_excerpt(); ?>
    	</div>
    <?php endwhile; ?>
    Thread Starter specialmachine

    (@specialmachine)

    Sorry, I was using this….

    <?php
    	$args=array(
    		'orderby' =>'parent',
    		'tag' => 'essen+trinken+wein',
    		'order' =>'asc',
    		'post_type' =>'page',
    		);
    	 query_posts($args);
    	$page_query = new WP_Query($args);
    ?>
    
    <?php while ($page_query->have_posts()) : $page_query->the_post(); ?>
    	<div class="teaser">
    	<h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
    	<?php the_excerpt(); ?>
    	</div>
    <?php endwhile; ?>
    Hakkim

    (@hakkimpahammed)

    Remove the comma after ‘post_type’ =>’page’ and try again. Also have you set tags for posts from admin side and save it? if not please do that and use the same tag name in the above code.

    Thread Starter specialmachine

    (@specialmachine)

    Thanks for sticking with me, Hakkim. I really appreciate it.

    This is currently the code I’m using, after having removed the comma after ‘post_type’ =>’page’

    It seems to only work with a single tag. If I add more than one, it stops working and nothing displays, although it doesn’t break the page.

    <?php
    	$args=array(
    		'orderby' =>'parent',
    		'tag' => 'essen+trinken+wein',
    		'order' =>'asc',
    		'post_type' =>'page'
    		);
    	 query_posts($args);
    	$page_query = new WP_Query($args);
    ?>
    
    <?php while ($page_query->have_posts()) : $page_query->the_post(); ?>
    	<div class="teaser">
    	<h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
    	<?php the_excerpt(); ?>
    	</div>
    <?php endwhile; ?>
    Hakkim

    (@hakkimpahammed)

    Try this way

    $args=array(
    ‘orderby’ =>’parent’,
    ‘tag_slug__in’ => array(‘essen’,’trinken’,wein’),
    ‘order’ =>’asc’,
    ‘post_type’ =>’page’
    );
    and remove this line query_posts($args);

    Thread Starter specialmachine

    (@specialmachine)

    In this case, would it be looking for a page that is tagged, ‘essen’, and ‘trinken’ and ‘wein’?

    Sorry. I’ve realized I can just tag all three pages with ‘menu’ and as they are set to order anyway, it really achieves what I’m after.

    That said, ideally, it would be great to be able to have an arbitrary order for design purposes. But this achieves what I’m after, so I do thank you. The reason I was after a way to display pages by slug was so I could just list them in the order I wanted them to appear, along with not having to fuss over an ID as they are often if ever in sync unless the databases are exactly the same between what’s online and my local machine where I build the sites.

    So thank you for your help. We can leave it there if you like. Perhaps some day I’ll achieve my dream. ??

    Thread Starter specialmachine

    (@specialmachine)

    Ah, just saw your new post. I’ll try it.

    Thread Starter specialmachine

    (@specialmachine)

    Boom! That works! THANK YOU! Now I have two very useful bits of code. Lots of love from Germany, Hakkim!

    Thread Starter specialmachine

    (@specialmachine)

    For anyone’s future reference, the code works in both manners as you would assume…

    <?php
    	$args=array(
    		'orderby' =>'parent',
    		'tag_slug__in' => array('food','cocktails','wine'),
    		'order' =>'asc',
    		'post_type' =>'page'
    		);
    	$page_query = new WP_Query($args);
    ?>
    
    <?php while ($page_query->have_posts()) : $page_query->the_post(); ?>
    	<div class="teaser">
    	<h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
    	<?php the_excerpt(); ?>
    	</div>
    <?php endwhile; ?>

    or…

    <?php
    	$args=array(
    		'orderby' =>'parent',
    		'tag_slug__in' => array('menu'),
    		'order' =>'asc',
    		'post_type' =>'page'
    		);
    	$page_query = new WP_Query($args);
    ?>
    
    <?php while ($page_query->have_posts()) : $page_query->the_post(); ?>
    	<div class="teaser">
    	<h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
    	<?php the_excerpt(); ?>
    	</div>
    <?php endwhile; ?>

    So, a nice use for this code could be displaying pages in a single template, like if you have a menu with three sections (food, cocktails, wine). So you can tag them all “menu” and use just that tag in the code, or you can control the order in an arbitrary way by listing the tags in the array.

    Thank you again to Hakkim. I’m really happy to know there are kind people out there, willing to help. It’s really cool.

    Hakkim

    (@hakkimpahammed)

    You’re welcome man…:) Enjoy !!!

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Listing specific page by slug’ is closed to new replies.