Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've been searching for how to do this for a while but to no avail. Part of my template is using a lot of the same code, i.e. retrieving a specific page.

If this was Javascript I'd set up a function and keep calling the function. How can I call the code below repeatedly?

<?php
    $page = get_page_by_title('Excerpts Showreel'); <-- This piece of code will change
// the code below will never change.
?>
<?php
    $my_id = $page;
    $post_id = get_post($my_id, ARRAY_A);
    $title = $post_id['post_title'];
    $content = $post_id['post_content'];
?>
<?php 
    echo $content
?>
share|improve this question
    
Can you clarify what you're trying to do? I'm not completely understanding your problem. –  Chip Bennett Jun 21 '11 at 19:38
add comment

3 Answers

up vote 0 down vote accepted

You can define your own custom functions in PHP just like you would in JavaScript. Here's your code example rewritten using a function:

$page = get_page_by_title('Excerpts Showreel'); <-- This piece of code will change

// the code below will never change.
function get_my_content( $page ) {
    $my_id = $page;
    $post_id = get_post($my_id, ARRAY_A);
    $title = $post_id['post_title'];
    $content = $post_id['post_content'];

    return $content;
}

echo get_my_content($page);

So long as you have your get_my_content() function defined, you can use it anywhere you need to.

share|improve this answer
    
Hey EAMann, that's great and it works. However, I'm having trouble echoing the Title, can you put an example of how to do that please? –  SparrwHawk Jun 21 '11 at 21:51
    
Having trouble echoing the title ... where and how? –  EAMann Jun 21 '11 at 22:02
    
Hi EAMann, see the reply I sent to Chip below with the pastebin. –  SparrwHawk Jun 21 '11 at 22:07
add comment

Following on from @EAMann's answer, I'm assuming you want 'Excerpts Showreel' to be variable, and to return content based on passing the variable?

function mytheme_get_page_content_by_title( $title = false ) {

    if ( false == $title ) return;

    $page = get_page_by_title( $title );
    $my_id = $page;
    $post_id = get_post($my_id, ARRAY_A);
    $title = $post_id['post_title'];
    $content = $post_id['post_content'];

    return $content;
}

Then in your template:

echo mytheme_get_page_content_by_title( 'Excerpts Showreel' );

Note that you might want to sanitize the $title variable being passed into the function.

EDIT

I'm not sure why you'd need to return the title (it's what you start with, and pass to your function), but here you go:

function mytheme_get_page_details_by_title( $title = false ) {

    if ( false == $title ) return;

    $page = get_page_by_title( $title );
    $my_id = $page;
    $post_id = get_post($my_id, ARRAY_A);
    $posttitle = $post_id['post_title'];
    $postcontent = $post_id['post_content'];

    $details = array(
        'title' => $posttitle,
        'content' => $postcontent
    );

    return $details;
}

Then you'd have to pass it to a variable to use it in your template:

$mypostdetails = mytheme_get_page_details_by_title( 'Excerpts Showreel' );

echo $mypostdetails['title']; // print the post title
echo $mypostdetails['content']; // print the post content
share|improve this answer
    
Thanks Chip that was helpful. Could you suggest how to return the title please... I have this at the moment pastebin.com/BzSA2skt (Can't seem to format code in this comment) It echos the content correctly but the title doesn't work, it spits out a previous title. –  SparrwHawk Jun 21 '11 at 22:06
    
Sure, that's not too difficult; but I'm curious why you need to return the title, since the title is what you pass to the function to begin with? –  Chip Bennett Jun 21 '11 at 22:10
    
Hi Chip, It's because I'm pulling in lots of pages into one single page. The reason I'm doing that is to abstract the code from the end user - don't want them to see div's etc. Here is a fuller snippet of some of my code where I'm pulling in three pages - perhaps you can see what I'm trying to achieve. I am open to suggestions, there may be a much better way of doing it since I am still a WordPress novice. pastebin.com/cLPsb74R –  SparrwHawk Jun 21 '11 at 22:15
    
I've updated my answer with a method for returning the title. –  Chip Bennett Jun 21 '11 at 22:15
    
In my paste bin above the pages are being pulled in successfully, but when I add echo get_my_content($title) it doesn't work. –  SparrwHawk Jun 21 '11 at 22:16
show 2 more comments

the function get template part will do this perfectly. Just put your code in a separate file.

<?php get_template_part( 'loop', 'index' ); ?>

With the above, you would create a file in your themes folder called loop-index.php

Read up on it here: http://codex.wordpress.org/Function_Reference/get_template_part

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.