WordPress.org

Forums

Pull in post numbers from Custom Fields (6 posts)

  1. Vero44
    Member
    Posted 2 years ago #

    Hi. I am trying to pull in the posts that I request in a custom field as related titles. I'm directly editing my templates.

    Example: Custom field "relatedposts" has input of "105, 107, 108, 112"

    These are the IDs of 4 posts that I want to display the titles of, underneath my post.

    So far (in index.php) I have this:

    <?php
    $post_id = 105;
    $queried_post = get_post($post_id);
    ?>
    <h3><?php echo $queried_post->post_title; ?></h3>

    This works but obviously doesn't pull in the array set by the custom field. I'm not too hot on arrays, how can I achieve this?

    Thanks in advance.

  2. alchymyth
    Forum Moderator
    Posted 2 years ago #

    pull in the array set by the custom field

    looks more like a string with comma separated numbers;

    try for example:

    <?php
    $postlist = explode( ',', get_post_meta($post->ID, 'relatedposts', true) );
    
    if($postlist) foreach( $postlist as $post_id ) {
    $queried_post = get_post($post_id);
    ?>
    <h3><?php echo $queried_post->post_title; ?></h3>
    <?php } ?>

    http://codex.wordpress.org/Function_Reference/get_post_meta
    http://php.net/manual/en/function.explode.php

  3. Vero44
    Member
    Posted 2 years ago #

    Thanks alchymyth, that works nicely.

    Do I need to set a condition, eg only process this if the 'relatedposts' custom field contains any content? I can write that code but can't see if it's necessary here. Just trying to think of ways it could break too, eg if a user puts a bad character in there.

  4. alchymyth
    Forum Moderator
    Posted 2 years ago #

    it might be good to check that the custom field is set, and that $post_id is not empty;

    for example:

    <?php
    $post_ids = get_post_meta($post->ID, 'relatedposts', true);
    $postlist = explode( ',', $post_ids );
    
    if($post_ids && $postlist) foreach( $postlist as $post_id ) {
    if( !post_id ) continue;
    $queried_post = get_post($post_id);
    ?>
    <h3><?php echo $queried_post->post_title; ?></h3>
    <?php } ?>
  5. Vero44
    Member
    Posted 2 years ago #

    Again, thanks for that. I have your code working but I've been trying to integrate the first image from the post as well as the title. I use the excellent code here:
    http://www.pcrepairmansblog.com/create-beautiful-post-displays-using-wordpress-theme-image-resize-snippet-teaser/
    which is great for pulling that image in AND resizing it. The function I am trying to integrate is the pcrmbimagegrab()

    How would I get this to display the image using this function?

  6. Vero44
    Member
    Posted 2 years ago #

    Can anyone help to echo the resized image this function outputs as well as the title? Here's the function.php code:

    function pcrmbimagegrab() {
    //http:www.pcrepairmansblog.com
    //Grabs the first image from the post
    global $post, $posts;
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    $first_img = $matches [1] [0];
    //Get default image instead
    if(empty($first_img)){
    $first_img = "/images/default.png";
    }return $first_img;}

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.