• Hi…

    I am adding a snippet to call related posts like so:

    <?php

    // ********* BEGIN HERE *********

    function cc_related_posts($args = array()) {

    global $post;

    // default args
    $args = wp_parse_args($args, array(
    'post_id' => !empty($post) ? $post->ID : '',
    'taxonomy' => 'category',
    'limit' => 3,
    'post_type' => !empty($post) ? $post->post_type : 'post',
    'orderby' => 'rand'
    ));

    // check taxonomy
    if (!taxonomy_exists($args['taxonomy'])) {
    return;
    }

    // post taxonomies
    $taxonomies = wp_get_post_terms($args['post_id'], $args['taxonomy'], array('fields' => 'ids'));

    if (empty($taxonomies)) {
    return;
    }

    // query
    $related_posts = get_posts(array(
    'post__not_in' => (array)$args['post_id'],
    'post_type' => $args['post_type'],
    'tax_query' => array(
    array(
    'taxonomy' => $args['taxonomy'],
    'field' => 'term_id',
    'terms' => $taxonomies
    ),
    ),
    'posts_per_page' => $args['limit'],
    'orderby' => $args['orderby'],
    'order' => $args['order']
    ));

    if (!empty($related_posts)) { ?>
    <div class="related-posts">
    <h3 class="widget-title"><?php _e('Related articles', 'textdomain'); ?></h3>

    <ul class="related-posts-list">
    <?php
    foreach ($related_posts as $post) {
    setup_postdata($post);
    ?>
    <li>
    <a class="title" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
    <?php if (has_post_thumbnail()) { ?>
    <div class="thumb">
    <?php echo get_the_post_thumbnail(null, 'medium', array('alt' => the_title_attribute(array('echo' => false)))); ?>
    </div>
    <?php } ?>
    <h4><?php the_title(); ?></h4>
    </a>
    </li>
    <?php } ?>
    </ul>
    <div class="clearfix"></div>
    </div>
    <?php
    }

    wp_reset_postdata();
    }

    Article says: The next step is to call the function?cc_related_posts()?wherever you want in your template. Usually, you’ll want to add it in the?single.php?file after calling?the_content().

    How do I add this to single post template in TT4 (Block theme). How do I call this function and place it where, since I have no single.php. I think

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator t-p

    (@t-p)

    I recommend asking at https://www.ads-software.com/support/theme/twentytwentyfour/#new-post so its developers and support community can help you with this.

    Moderator bcworkz

    (@bcworkz)

    Block themes do not have PHP templates, so you cannot call your function from a block template. Your options are to implement your PHP as a pattern, a shortcode, or a custom block.

    Thread Starter hebhansen

    (@hebhansen)

    @bcworkz So how do you add related posts in block themes if not php?

    How do I add the php as pattern or custom block?

    Moderator bcworkz

    (@bcworkz)

    For patterns, try this tutorial. The example implements a pattern with markup, but you’ll see that it’s all within a .php file, so your pattern content can come from PHP output instead of markup. The tutorial is oriented for front page templates, but you can target many other types of templates as well.

    For blocks, try this tutorial. The main problem with a custom block is you’ll need to covert your PHP logic into JavaScript. Because of this I’d personally either stick to shortcodes or patterns. But it is an option none the less.

    • This reply was modified 1 week, 6 days ago by bcworkz. Reason: corrected typo

    why dont you use a related posts plugin?

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.