Wordpress bringing in template_part based on custom field - php

I have a Wordpress loop on my index.php which brings in a specific template-part.
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
get_template_part( 'content' );
?>
<?php endwhile; ?>
<div class="clearfix"></div>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>
content.php has an article with the blog post information (title, featured image, etc) with specific html.
I have a custom-field, ExtraCSS, applied to each post through the content.php with values 'post-right' or 'post-left' (so I can change css of individual posts).
content.php code
<?php $extraCSS = get_post_meta(get_the_ID(),'ExtraCSS',true);
<article class="<?php echo $ExtraCSS?>">
...
I want to have 2 different template_parts which can get called with the loop depending on the custom-field or value applied to the post.
EX. so if the post has the ExtraCSS custom-field value 'post-right' applied to it, the loop will bring in
get_template_part( 'content' , 'right' );
if the post has the ExtraCSS custom-field value 'post-left' it will bring in
get_template_part( 'content' , 'left' );
This may not be the right way to do it, and I'm open to other suggestions, but thats the overall idea. Wanting two different post-templates for one loop There will be numerous posts that have either one, and I want them all brought in one after the other.

As you said it's either one or the other template:
<?php
$extraCSS = get_post_meta( get_the_ID(), 'ExtraCSS', true );
if ( $extraCSS == 'post-right' ) {
get_template_part( 'content' , 'right' );
}
else {
get_template_part( 'content' , 'left' );
}
?>

Related

Filter the posts by a specific author

I want to display only one author's post say "abc_author" .
i have use the validation using if clause but THE PROBLEM is that i get pagination below the post where page 1 is blank (as an other author post is skipped) , page 2 (as an other author post is skipped) is blank and soon i get my desired post on page 6 . commenting the verbosa_pagination() hide the pagination navigation but the starting pages are blank and
I am a new to WordPress , i have also tries different plugin but they facilitate me to filter it on pages not post (though i can change the home page to a particular page but i want it on default blog post for few reasons )
<?php if ( have_posts() ) : ?>
<div id="content-masonry">
<?php /* Start the Loop */
while ( have_posts() ) : the_post();
if(get_the_author()=='abc_author')
{
get_template_part( 'content/content', get_post_format() );
}
endwhile; ?>
</div> <!-- content-masonry -->
<?php verbosa_pagination();
else :
get_template_part( 'content/content', 'notfound' );
endif;
Can you please try this code before the while loop?
<?php query_posts( array( 'author' => 'your author ID' ) ); ?>
Ref: https://developer.wordpress.org/reference/functions/query_posts/#usage

Wordpress one-page template with dynamic portfolio section

I've been searching all morning for answers to no avail. I'm building a one page WordPress template. I have a home page which uses a one page template called one-page.php that is brining in all the other pages. Heres the php from that template:
<?php
$args = array(
'post_type' => 'page',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; endif; ?>
This code works great. All the sections can use the content-page.php template part, excluding the portfolio section which I would like to use another template part that brings all the portfolio custom post types.
I've tried to add conditional if statements to both the one-page.php and the content-page.php, like this:
<?php if ( is_page( 'portfolio' ) ) : ?>
//My portfolio custom post type loop is here
<? endif; ?>
But that didn't work either - I think that is because the is_page() function will be checking the current page being displayed which is the Home page. Rather than figuring out what page the query is currently dealing with - but I'm not sure.
Can anyone help me understand how I would go about conditionally loading the portfolio section into a separate template part?
You can achieve this checking page slug, which you can get in the loop. If it is "portfolio" (or whatever you saved), load content-portfolio.php, otherwise content-page.php. Something like this:
if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
if ("portfolio" === $post->post_name) {
get_template_part('content', 'portfolio');
} else {
get_template_part('content', 'page');
}
endwhile; endif;
Set condition in file
<?php
if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
if (is_page('portfolio')) {
get_template_part('content', 'portfolio');
} else {
get_template_part('content', 'page');
}
endwhile; endif;
?>
create a template file named content-portfolio.php which is copy of content-page.php and put the below code in it. if it show 'call' it means your template is working.
portfolio.php
<?php
echo "portfolio.php";
die('Call');
?>

Need help using different 'page-templates' for one-page wordpress site sections

This is my second question, and I promise as soon as I feel like I can help others, I'm gonna return the favours to the community!
I'm building a one page Wordpress site with different sections, working from _S theme and learning as I go.
I already have all my different pages being pulled into the front-page, but I would like the different sections to have different layouts and elements. To be specific - in one section I would like content to be pulled in along side an iFrame that float next to each other. To do this I know that I need to call a custom page template that specifies two floating div containers (I'm building using Bootstrap).
In the front-page.php, I've written this:
<?php
if ( get_option( 'show_on_front' ) == 'posts' ) {
get_template_part( 'index' );
} elseif ( 'page' == get_option( 'show_on_front' ) ) { ?>
<?php get_header(); ?>
<section class="home">
<div class="entry-content">
<?php
$args = array(
'post_type' => 'page',
'order' => 'ASC'
);
$the_query = new WP_Query($args);
?>
<?php
while($the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if (is_page(9)) : ?>
<?php get_template_part('play','page');?>
<?php else : ?>
<?php get_template_part('content', 'page');?>
<?php endif; ?>
<?php endwhile; ?>
</div>
</section>
<?php
get_footer();
}
?>
Which works perfectly in bringing in my different page sections, but I can't see how to call a different for the page (called 'play' or with the ID of 9).
I have already created a template called play-page
Any ideas?
Thanks masses
Harry
Let's assume you want to display 3 sections (about, work, contact) on the custom front page, you can create per page template for each section, like:
section-about.php
section-work.php
section-contact.php
In page-about.php like this:
<div class="section-about">
<?php the_content(); ?>
</div>
And use section-work, section-contact for other 2 pages.
Then you create 3 pages in the Dashboard and assign each to its template.
Now, you can add the loop in your font-page.php
<?php
$args = array(
'post_type' => 'page',
'post__in' => array(1, 2, 3) // the ids of those pages
);
$the_query = new WP_Query($args);
?>
<?php while($the_query->have_posts()) : $the_query->the_post(); ?>
<?php
the_title();
the_content();
// etc
?>
<?php endwhile; wp_reset_postdata(); ?>

Override default setting for category archive output (in twentytwelve)

I have this snippet in a custom category template which outputs the last X posts of that category.
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); get_template_part( 'content', get_post_format() ); endwhile; twentytwelve_content_nav( 'nav-below' ); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
Problem is this outputs the posts as excerpts.. How can I change it to the_content() without affecting all the other loops? I only need to have the_content here.
Do I need to manually output the title, then the date, then author and then the_content() or can I just replace something in the snippet above ?
From what you have given, it seems that you have customized the content.php template.
To show the content just for your category page, look, you'll need to add that condition. As you did not add the code for content.php, I can't exactly say where to add it, but you have to do somthing like this
if(is_category()) {
the_content();
}else{
the_excerpt();
}

How to display more listings in WordPress

I'm having an issue with the listings in the WordPress site I'm working on.
I have three listings only showing up out of 6. I can't seem to figure out how to make all of them display. This is using the twentyeleven WordPress theme.
The arrows on the right are used to move the gallery back and forth. Only one more shows up on the right side.
Here's the code I believe is generating it.
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php if ( is_home() ) {
query_posts($query_string . '&cat=-3');
}
?>
<?php
$page_name="Articles";
$page=get_page_by_title($page_name);
//echo $page->ID;
query_posts( 'cat=-1,-2' );
?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
</div>
Any help would be great, thanks.
Change your query_posts() function to the following:
query_posts( 'cat=-1,-2&posts_per_page=6' ); // You can change the post_per_page variable as needed
However, I would suggest using an $args array instead of a querystring to make your query. The same query would look like this:
$args = array(
'cat' => array( -1, -2 ),
'posts_per_page' => 6
);
query_posts($args);
It is much more readable and easier to update. Also, it's worth mentioning, you are adding a negative operator to your categories. In the query_posts function, that will exclude a category. You may only be getting 3 posts because you are excluding posts from your query.

Categories