Forums

[resolved] Using The Loop with the backend editor (11 posts)

  1. RyanJenkins
    Member
    Posted 3 years ago #

    Hey all.

    I have a handful of static pages on the site that are profiles of people. On each of their pages, I want to generate lists of recent blog posts that have to do with them – let's say I'll just query by the relevant tag.

    The problem is that I can't figure a way to insert a loop, mini-loop, query_posts() or anything on a static page when I'm using the backend (Administrator access) editor. It just doesn't accept PHP, and I tried with a simple <?php echo("Hello, world!") ?>. No dice.

    Am I doing something wrong or is it certainly the case that there's no way you can insert PHP into the backend editor? Am I going to have to just write their pages by hand and put in a mini-loop like I would on the homepage, call them profile1.php, profile2.php, etc., and then insert hardcoded links to them, and forfeit the ability to edit them with the backend?

    Thanks.

  2. stvwlf
    Member
    Posted 3 years ago #

    Hi

    Use a custom page template for that page, and include the content including PHP in that template
    http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates

  3. RyanJenkins
    Member
    Posted 3 years ago #

    So, since I have several different pages that will require different categories of posts be loaded, will that template have a big if/else structure like this in it, one that is manually maintained? I took this example off of the page you sent:

    <?php
    if (is_page('21') ) {
    $cat = array(12);
    } elseif ( is_page('16') ) {
    $cat = array(32);
    } elseif ( is_page('28') ) {
    $cat = array(17);
    } else {
    $cat = '';
    }

    "A Page of Posts," I think that's right. Although, I'm going to have to have two loops there, I'm going to have to have the loop that displays the regular the_content or the_post from that page, and then also generates this little list off to the side... Am I on the right track? Thanks for the info.

  4. stvwlf
    Member
    Posted 3 years ago #

    Hi
    The code goes in the custom template, not in the post area

    If this is a second loop, it looks like this

    <?php
    $cat = '';
    if (is_page('21') ) {
      $cat = 12;
    } elseif ( is_page('16') ) {
      $cat = 32;
    } elseif ( is_page('28') ) {
      $cat = 17;
    }
    
    if ($cat) {   // if a category has been set, display 5 posts
      $profiles = new WP_Query();
      $profiles->query("showposts=5&amp;cat=$cat"); ?>
      <ul>
         <?php while ($profiles->have_posts()) : $profiles->the_post(); ?>
         <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
        <?php endwhile; ?>
      </ul>
    <?php }  ?>
  5. RyanJenkins
    Member
    Posted 3 years ago #

    Excellent, thanks for the code. I modified it a little to fit my needs, and it's looking great. I just put a mini-version on my Press Releases page to pull from that category, and it works just fine. Now I get to have fun formatting the output. I'll be back here if I run into problems with the full deployment in a day or two. Thanks again! Ryan

  6. RyanJenkins
    Member
    Posted 3 years ago #

    Oh, also, it wasn't filtering categories correctly when I first implemented it and I had to figure out why. Here is why. On this line:

    $profiles->query("showposts=5&amp;cat=$cat"); ?>

    I had to change the &amp; to an actual ampersand (&amp;). This might have been the fault of the browser you were using to post or something. Just FYI.

    Edit: my browser just did it, too. Maybe it is the fault of WP's forums.

  7. stvwlf
    Member
    Posted 3 years ago #

    Hi

    As of the last month or two the forum software changes all ampersands in code to & a m p ; Its been requested several times that this be changed but it still does it. I try to remember to advise people of that when I post code, but sometimes I forget.
    Sorry

    Glad its working well for you!

  8. RyanJenkins
    Member
    Posted 3 years ago #

    Now I am looking to do the same thing, except with tags instead of categories.

    Here is my code, largely adopted from this Codex page: http://codex.wordpress.org/Template_Tags/query_posts

    <?php
    		$tag = '';
    		if (is_page('36') ) {			// Larry Bostic		tag 30
    		  $tag = 30;
    		} elseif ( is_page('38') ) {	// Orlando Boquete	tag 31
    		  $tag = 31;
    		} elseif ( is_page('11') ) {	// Alan Crotzer		tag 32
    		  $tag = 32;
    		// ...etc...
    
    		echo($tag);
    
    		if ($tag) {
    		  $profiles = new WP_Query();
    		  $profiles->query("tag=$tag"); ?>
    		  <ul> <!-- Put an if statement to have a header "Related news items about $title..." -->
    		     <?php while ($profiles->have_posts()) : $profiles->the_post(); ?>
    		     <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    		    <?php endwhile; ?>
    		  </ul>
    	<?php }  ?>

    The problem is that I can't find an example of query being run and using only the tag ID number. This is a bit easier, I think, but I could just as quickly use a string that's the actual tag's name. So, instead of using $tag = 30; for Larry Bostic, using$tag = 'Larry Bostic'`.

    First, is there a way just to do this using the tag ID, which I think is more reliable/trustworthy than using the actual string? If there's not, then I'd like an example of using the syntax to define $tag, and then passing it or using it to query.

    Thanks!

  9. RyanJenkins
    Member
    Posted 3 years ago #

    Okay, so I got this:

    if (is_page('36') ) {			// Larry Bostic		tag 30
    $tag = 'Larry-Bostic';}
    
    ...
    
    $profiles->query("tag=$tag"); ?>

    And that works just fine. Before I go this route completely, is there a way to pass just the tag ID number? Again, I feel more comfortable passing numbers than strings. Thanks.

  10. stvwlf
    Member
    Posted 3 years ago #

    did you try it using just the tag ID?

    if (is_page('36') ) {			// Larry Bostic		tag 30
    $tag = 30;}
    
    ...
    
    $profiles->query("tag=$tag"); ?>

    It is likely to work that way as well

  11. RyanJenkins
    Member
    Posted 3 years ago #

    That did not work. I am content, for now to leave it like this:

    if (is_page('36') ) {			// Larry Bostic		tag 30
    		  $tag = 'Larry Bostic';
    }
    
    ...
    
    $profiles->query("tag=$tag");

    Appreciate it. This page likely won't be expanded too often, so it's alright to keep doing it manually. I will mark as resolved. Ryan

Topic Closed

This topic has been closed to new replies.

About this Topic