WordPress.org

Forums

[resolved] Individual post pages custom content (7 posts)

  1. Richard Sweeney
    Member
    Posted 5 years ago #

    Apologies if this has already been answered elsewhere, but I haven't been able to turn up anything yet.

    I'm using WP as a CMS for a website I'm working on (still locally, nothing online to show as yet!). I'm displaying posts on a page called 'news' (that's all working fine) but I want to add some additional HTML to the pages where the individual posts are shown.

    Basically, all I want to do is to link back to the 'news' page from the child pages (the individual posts). But I obviously don't want the link to appear on the parent page (the news page).

    I've found a (seemingly working) function to find sub-pages:

    function is_subpage() {
     global $post; // load details about this page
     if ( is_page() && $post->post_parent ) { // test to see if the page has a parent
          	$parentID = $post->post_parent; // the ID of the parent is this
            return $parentID; // return the ID
    	} else { // there is no parent so...
          	return false; // ...the answer to the question is false
    };
    };

    But have not been able to get it to work - I've tried using the following code:

    <?php if(is_subpage('news')) : ?>
    <p><a href="<?php bloginfo('url') ?>/news/">Read all news stories</a></p>
    <?php endif; ?>

    Any help greatly appreciated!

  2. equalmark
    Member
    Posted 5 years ago #

    So have you got a page called 'News' and you have set this as your posts page in the WordPress 'Reading' setting page?

  3. Richard Sweeney
    Member
    Posted 5 years ago #

    Hi,

    Yeah exactly. My page called 'news' displays the posts.

  4. equalmark
    Member
    Posted 5 years ago #

    That page is therefore controlled by your themes index.php file. Therefore if you add the following:

    <p><a href="<?php bloginfo('url') ?>/news/">Read all news stories</a></p>

    before the end of the loop inside <div class="post"> it should do the trick.

  5. Richard Sweeney
    Member
    Posted 5 years ago #

    thanks for this, I guess I'm not explaining myself very well!

    I don't want the hyperlink to appear on the 'news' page, but I want it to appear on any child pages.

    For example: the page:

    http://www.mysite.com/news/

    shouldn't have the link, but the page:

    http://www.mysite.com/news/some-news-post-or-other/

    should have the link. Know what I mean?

    Cheers

  6. equalmark
    Member
    Posted 5 years ago #

    Add it to single.php then in the same place.

  7. Richard Sweeney
    Member
    Posted 5 years ago #

    Now I'm just flaunting my inexperience! I didn't have a single.php page for the site, I do now and all is as it should be!

    Many thanks equalmark, most helpful!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags