WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Including the Same Content on Multiple Pages (12 posts)

  1. foochuck
    Member
    Posted 3 years ago #

    I'm working on a WordPress site that is going to cover two sub-sites included under one main site.

    Some of the pages on the two sub-sites will have duplicate page content where I'd like to pull from the same content for both pages. This will also allow me to edit the duplicate content in one place so that it updates on every page that uses that content.

    Here's an example:

    Site 1 About Us Page: http://www.mysite.com/site-1/about-us

    Site 2 About Us Page: http://www.mysite.com/site-2/about-us

    So although these are two different pages, I'd like to pull the same content to go on each of these pages. Is there a trick to do this or a plugin that someone knows of that could help me accomplish this?

    If I were doing this outside of WordPress, I'd simply do a PHP include and pull from the same file on each page - however I'm not sure how to accomplish that with WordPress.

    Thanks

    -foo

  2. vtxyzzy
    Member
    Posted 3 years ago #

    One possibility - create a Custom Post type for shared posts and use a special query to include the same custom post on every page where it is needed.

  3. foochuck
    Member
    Posted 3 years ago #

    @vtxyzzy - How can I create a custom post type?

    I'm also not sure what query I can use to include a custom post. Do you know of any examples I could use for this?

  4. vtxyzzy
    Member
    Posted 3 years ago #

    The Custom Post Type UI plugin makes it easy to create custom post types.

    As for the query, it is just like any other query except that you would use the 'post_type' argument as shown in the Codex here under Page Parameters.

  5. foochuck
    Member
    Posted 3 years ago #

    For the query, do I need to make a page template or can I run that query in the content area of my pages using PHP?

  6. vtxyzzy
    Member
    Posted 3 years ago #

    Not quite sure what you are asking, but I think you could do it either way. Try to think of it as a standard query, just using one different argument (post_type). Where ever you would put a regular query, you can put the custom one.

  7. foochuck
    Member
    Posted 3 years ago #

    Option 1. Can I put a regular query right into the content of a page?

    Option 2. Would I need to create a page template and insert the query in the template since it's using PHP?

    From my experience, I don't think you can use PHP in the content of a page or post, so I'm guessing I have to go with option #2, since I want the same content to appear on multiple pages. I would apply the page template to those pages.

  8. foochuck
    Member
    Posted 3 years ago #

    Okay here's what I've tried.

    I created a custom post type called "builders".

    Under builders, I created a post called "associate builders".

    Now for the page template I created, I'm trying to query it like so:

    <?php query_posts( array( 'post_type' => array('builders', 'associate builders') ) ); ?>
    
    				<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    
    					<div class="entry-content">
    						<?php the_content(); ?>
    						<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
    						<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
    					</div><!-- .entry-content -->
    				</div><!-- #post-## -->

    This query pulls no data. So first off, I think I'm not doing the query correctly. Any suggestions on this?

  9. vtxyzzy
    Member
    Posted 3 years ago #

    Under builders, I created a post called "associate builders".

    The query is not quite right. 'associate builders', based on your comment above, is a post, not a post_type. Therefore, you do not include it in the array of post_types.

    <?php query_posts( array( 'post_type' => array('builders') ) ); ?>

    Option 1. Can I put a regular query right into the content of a page?

    Option 2. Would I need to create a page template and insert the query in the template since it's using PHP?

    Option 1 is possible using a plugin like Exec PHP, but I would recommend Option 2.

  10. foochuck
    Member
    Posted 3 years ago #

    Okay, I've updated my code on my page template:

    <?php query_posts( array( 'post_type' => array('builders') ) ); ?>
    
    				<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    
    					<div class="entry-content">
    						<?php the_content(); ?>
    						<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
    						<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
    					</div><!-- .entry-content -->
    				</div><!-- #post-## -->

    The page using this template is showing no content, although I do have a post setup at:

    http://www.mysite.com/builders/associate-builders

    And that post displays content. Something else I could be doing wrong? Perhaps I need a loop on this page?

  11. vtxyzzy
    Member
    Posted 3 years ago #

    You definitely need a loop after all query_post() calls. You probably also want to retrieve only 1 post, so add the posts_per_page argument to the query:

    <?php query_posts( array( 'post_type' => array('builders'), 'posts_per_page' => 1 ) );
    if (have_posts()) {
       while (have_posts()) {
          the_post();?>
          <!-- rest of your code from above here -->
       <?php } // endwhile
    } // endif ?>
  12. foochuck
    Member
    Posted 3 years ago #

    Ahhhhhh. Works like a charm. Thanks so very much vtxyzzy!

    -foo

Topic Closed

This topic has been closed to new replies.

About this Topic