Wordpress conditional post layout - php

I need to display multiple styles of posts; News1 and News2. The problem i am having is that i need to display each post in a different style. IE for posts that are in News1 they need to have date and share buttons but for News2 i don't need this.
I have a custom field set up to section News2 off called "Archive Category" and can use the get_post_meta for this.
My problem is just the conditional statements that should be in the posts file.
If anyone can help it would be greatly appreciated.
Thanks

In single.php, get the custom field value, then according to the value, call different template part:
get_template_part( 'prefix', 'other-parts-of-the-file-except-extension' );
In the template part files, layout the single post/page differently.
To elaborate, the actual layout code shall be written in template part files. The single.php only checks the custom field value, and call different template part file according to the value.
Pseudo-code would be (in single.php or single-post.php or single-customposttype.php, whichever you're using):
<php
$value=get the custom field value; // replace this with your function to get the value
if($value=='News 1')
get_template_part('template', 'news1'); // then you'll put layout in template-news1.php
else
get_template_part('template', 'news2'); // layout in template-news2.php
?>

Related

Archive.php Category.php or Taxonomy.php - Which is correct for my situation?

I'm using Genesis framework and I have this page (http://staging.seedcreativeacademy.co.uk/short-courses/) showing the categories of my custom post type short_courses. I have changed the category name to course_type, by creating a new custom taxonomy.
This is how I want it to work so far (styling needs sorting out admittedly!) Im also using CPT UI plugin.
Now, when I click through to a category, is displays each 'Course in a nice masonry block as you will see here: http://staging.seedcreativeacademy.co.uk/course_type/digital-marketing/
However, I dont want this pages to look like this and I've tried adding custom template for the following:
Archive-short_courses.php & taxonomy-short_courses.php
Archive-course_type.php & taxonomy-course_type.php
But it doesnt seem to alter the layout at all...
Once I pass this hurdle I will want to alter the single.php page for these short courses, but I thought I would start with this first.
Im not sure if genesis blocks this and sets a site wide default? I know if sets a site wide default for the archive settings but I cant find anything about a template, plus i dont know if I shoujld be searching for tutorials on archive.php pages, category.php pager or taxonomy.php pages...
Can someone help me clarify things please?
course_type is a term name, not taxonomy name.
So, these are correct for your case:
category-course_type.php (category-{slug}.php is correct format. So check if course_type is correct slug of that category)
single-short_courses.php
Just in case, try reload permalinks via Settings->permalinks->save after making these changes.
Looks like your theme or some plugin is adding masnory class to body tag, which then is styled by your child theme. You need to filter that class out of your body tag and then might styling goes to non-masonary styling.
Add following code to your taxonomy-course_type.php file, and also make sure you have genesis(); call as the last thing in the template.
add_filter('body_class', 'remove_body_class', 20, 2);
function remove_body_class($wp_classes)
{
foreach($wp_classes as $key => $value)
{
if ($value == 'masonry') unset($wp_classes[$key]);
}
return $wp_classes;
}
Above could should be in custom taxonomy template, which also have genesis(); as last line.

Show full post (not excerpt) for defined posts in wordpress

I'd like to have the possibility to define some posts in wordpress where the full post is shown and not an excerpt.
There are various solutions like:
Use a hidden html to declare this and code into the theme to either use the_content or the_excerpt
Hardcode into the theme (if postid == xx then the_content else the_excerpt)
Use post meta data and add "if" into theme to check for them
Create a plugin which adds the functionality automatically and also a checkbox "Always show full" into the post-editor.
The first one is easy but ugly, 2nd one should be doable with some googling but the 3rd one seems to be the most appealing to me but I have no idea how to achieve this.
Since usually in the template all i have is somewhere the_excerpt method from wordpress. I therefore should somehow inject some code there and check if the checkbox for the current post is set and then just use the_content instead. Is this possible at all or do I need to modify the theme anyway?
Thanks for your inputs.
I would use custom fields, it's more flexible, so you don't need to hard code the page ids.
<?php
$show_full_content = get_post_meta($post->ID, 'show_full_content', true);
if ($show_full_content == 'yes') {
the_content();
} else {
the_excerpt();
}
?>
You might be interested in ACF, it can make it more user friendly.

Conditionally display menu for page type in Wordpress

I am relatively unfamiliar with Wordpress and I am creating a custom theme for a client. I would like to either display or remove the main menu depending on the page type. I have researched several options like removing the navigation from header.php and referencing it separately and also making the menu conditional which is preferable.
I have a custom page type in my theme called 'landing page' on which I would like the menu to be never be displayed, though it will be on every other page. Ultimately there will be a lot of these and I would rather I didn't have to intervene.
I would rather not duplicate my header.php file but I can only find reference to displaying the menu conditionally like below by page name or ID which seems ridiculous.
<?php
if (is_page('contact')){
<?php include(TEMPLATEPATH.'/headerA.php'); ?>
}
elseif (is_page('gallery')){
<?php include(TEMPLATEPATH.'/headerB.php'); ?>
}
else {
<?php include(TEMPLATEPATH.'/headerA.php'); ?>
}
?>
Rather than including files as above, I will put the whole thing into my header and just make the navigation conditional. Does anyone know how I should approach this using my custom page type landing page rather than by page name so every page created with that type will never have a menu?
Thanks
Are you talking about a Custom Post Type (CPT) or a page called landing-page?
They are completely different. See http://codex.wordpress.org/Post_Types
In any event, this will work for a custom post type or a page:
if ( !is_singular( 'custom-post-type-name-or-page-slug-here' ) ) {
get_template_part('menu');
}
It says: "If this page is not a single page or a CPT, load the file menu.php from the theme folder."
See also http://codex.wordpress.org/Include_Tags:
The get_template_part() tag includes the file {slug}.php or
{slug}-{name}.php from your current theme's directory, a custom
Include Tags other than header, sidebar, footer.

Custom link and name meta box in wordpress

I would like to create a metabox in the section of edit post menu. Like Tags for exemple, BUT instead of inserting tags I would like to put a link (ex. http://www.google.com) + name.
This link should go right to the end of my post content as a hyperlink with "Source: Name (hyperlinked by specified link)
Here is an example of what I am needing:
Title
This post is for example
Source: Google
Any help would be much appreciated. Thanks guys.
You can use the Custom Fields in Wordpress posts to add extre information to a post, then in your theme you can just grab the values. Here is what I use on my blog:
$custom_fields = get_post_custom($post_id);
if(!empty($custom_fields['article_source_url'][0]) && !empty($custom_fields['article_source_title'][0])){ ?>
<strong>Source</strong>: <?php echo($custom_fields['article_source_title'][0]); ?>
<?php }
Then in your wordpress post just add a Source URL and Title in the custom fields box:
You can use WP's Custom Fields to denote specific parameters for each page/post.
Then, in the template, just call them like this:
<?php
$custom_fields = get_post_custom();
var_dump($custom_fields);
?>
Note that they are arrays, so in case you have a field named URL, you'd need to call it like
echo $custom_fields['URL'][0];
This is done like this so you can have multiple values for the same field.
Also, in order to see the Custom Fields box, you need to go to the Screen Options (top right of the edit page) and enable them.

Adding custom fields to the very bottom of the page in WordPress

I've made a template for specific pages of our website that display similar content, yet are different form the rest of the pages of the website - here's an example. I have several custom fields that I use to include all of the items you see in the boxes that float to the left and right.
My problem is, I also have pricing that's included from a custom field - and because I want it at the bottom of the page, it actually appears outside the content to work (see the bottom of my example given above). Is there a way I can call the content and tell it to display these custom fields at after the content is displayed?
Obviously the following doesn't work, but just to give you an idea:
<?php the_content(get_post_meta($post->ID, "Pricing_Mexico", true); ?>
Additionally, there are actually a lot of custom fields that need to be displayed, so if it were possible to include them in bulk (including the markup which includes divs and what-not) that would be preferred. Thanks!
You could filter the_content() so in your functions file:
function contentFilter($content){
// Output the current content
echo $content;
// Output the meta field
echo get_post_meta($post->ID, "Pricing_Mexico", true);
}
add_filter('the_content', 'contentFilter');
You could echo as many meta fields as you need inside the function.

Categories