Question

I'm building a blog for myself and I have made a custom page-template.php. I'm using the Wordpress query_posts loop like this:

Well, I got a template called page-video.php and I'm using this query!

<?php query_posts("cat=65"); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <article class="left-container-video"<?php post_class() ?> id="post-<?php the_ID(); ?>">
        <header class="video-header">
            <a href="<?php the_permalink() ?>"><h3> <?php the_title(); ?> </h3></a>
        </header>

             <div class="video-container">
                 <iframe src="<?php the_field("link"); ?>" frameborder="0" width="560" height="315"  allowfullscreen></iframe>
            </div><!-- end vid container -->

     </article>
<?php endwhile; ?>

<?php else : ?>

    <h2>Sidan hittades inte</h2>

<?php endif; ?>
<?php wp_reset_query(); ?>

Is there a way to change the cat=65 value to something else with Advanced Custom Fields?

I want to use same template on different Wordpress pages but I want to show different categories.

Was it helpful?

Solution

Dude, You can do lot of things for that. If I am in your seat i will do like this.

Here is the code. In this I used category slug you can use the id also.

In your function.php file add this

/*------------------------------------------*/
/**  [media] shortcode function - by Yesh  **/
/*------------------------------------------*/
function mediaShortcode($atts) {
  //Extract Shotcode from the pages and posts
  extract(shortcode_atts(array('slug' => 'default'), $atts));
  global $post;
  $args = array( 'numberposts' => 5, 'category_name' => $atts['slug'], 'orderby'  => 'post_date', 'order' => 'DESC');
  //print_r($args);
  $posts = get_posts( $args );
  //print_r($posts);
  $html="";
  foreach ($posts as $post) {
    $html.='<article class="left-container-video">';
      $html.='<header class="video-header">';
        $html.='<a href="'.$post->guid.'"><h1>'.$post->post_title.'<h1></a>';
      $html.='</header>';
      $html.='<div class="video-container">';
        $html.=$post->post_content;
      $html.='</div><!-- end vid container -->';
    $html.='</article>';
    //$html.=do_shortcode($post->post_content);
  }
return $html;
}
add_shortcode('media', 'mediaShortcode');

in your template.php

<?php
/*
Template Name: Media page Template
*/
?>
<?php get_header(); ?>
<div class="wrapper-small">
    <div style="" id="media-page">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <?php the_content(); ?>
    <?php endwhile; ?>
        <!-- <div class="navigation">
            <div class="next-posts"><?php next_posts_link(); ?></div>
            <div class="prev-posts"><?php previous_posts_link(); ?></div>
        </div> -->
    <?php else : ?>
        <h1>Not Found</h1>
    <?php endif; ?>
  </div>
</div><!-- end wrapper -->
<?php get_footer(); ?>

and now, as my function. I have to add in the page [media slug="video-category"]. Select page template as Media Page template. Thenpublish. If you want to show more category posts. Just add after the first line your next category slug.[media slug="video-gallery]`.

This will show the rest of posts in your gallery. You have to select the theme only. You can use this in any page.

For more information refer these articles.

If you need my help. Please find me any of social network by searching yeshansachithak.

OTHER TIPS

You can simply include a PHP file in each of your custom templates:

partial-include.php:

<?php query_posts("cat=" . $category_id); // category is not hardcoded ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <article class="left-container-video"<?php post_class() ?> id="post-<?php the_ID(); ?>">
        <header class="video-header">
            <a href="<?php the_permalink() ?>"><h3> <?php the_title(); ?> </h3></a>
        </header>
        <div class="video-container">
            <iframe src="<?php the_field("link"); ?>" frameborder="0" width="560" height="315"  allowfullscreen></iframe>
        </div><!-- end vid container -->
    </article>
    <?php endwhile; ?>
<?php else : ?>
    <h2>Sidan hittades inte</h2>
<?php endif; ?>
<?php wp_reset_query(); ?>

Then in each of your template files:

custom-template-1.php:

<?php
/*
Template Name: Custom Page 1
*/
get_header();
?>

    // ....

    <?php
        $category_id = 123;
        include('partial-include.php');
    ?>

    // ....

<?php get_footer(); ?>

You can repeat this for each of the pages that need the functionality.

If you need to integrate this with custom fields from Advanced Custom Fields, you can change the value of $category_id by doing the following in your custom-template-1.php file:

<?php
    $category_id = get_field('category_id'); // example
    include('partial-include.php');
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top