I work in multisite
I have a child theme of my parent theme to customize a page template.
In this customize page template I would like to display a shortcode, which allows to retrieve the url address of the site in question.
My function in my child theme file, function.php:
add_action( 'init', function() {
add_shortcode( 'site_url', function( $atts = null, $content = null ) {
return site_url();
} );
} );
I would like to declare this shortcode in my template customizer, like this:
/*
Template name: Test
*/
get_header(); ?>
<div>
<h1>Notre site web : [site_url]</h1>
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
get_template_part( 'content', 'page' );
endwhile;
endif;
?>
</div>
<?php get_footer(); ?>
````but the shotcode doesn't generate anything at all in my customize template.
I must have missed something... :-(
Can you help me ?
Thanks
This is because shortcodes that are hardcoded into templates don't get rendered by default. What you want to do is put the shortcode in the function do_shortcode() and then it should output correctly.
echo do_shortcode('[site_url]');
PICTUREI have short code <?php echo do_shortcode('[wonderplugin_slider id=1]'); ?> and have to put it above the page see in the picture
The issue is the slider (shortcode) should display JUST on Homepage or Main page.
I have tried many plugins and codes like this <?php if(is_page(<111>)) { ?><?php }>but it does not work
You can use is_front_page(), is_home() and is_page() functions.
In your code, you want to mention the page id directly means please put it without any quotes like below.
<?php
if(is_page(111)){
echo do_shortcode('[wonderplugin_slider id=1]');
}
?>
Or else you already set the page with id 111 as the home page or front page means, you can use is_home() and is_front_page() function.
if( is_front_page() ) {
echo do_shortcode('[wonderplugin_slider id=1]');
}
or
if( is_front_page() && is_home() ) {
echo do_shortcode('[wonderplugin_slider id=1]');
}
Thanks.
You shpuld check both is_home() and is_front_page(), but their usage depends on the frontpage you have. If you have a statick frontpage you can have:
if( is_front_page() ) {
...
}
In the other way around you can conbine both like:
if( is_front_page() && is_home() ) {
...
}
I have a wordpress website in which there is a custom theme where I can create template parts for each webpage. I want to include/inject a specific class (ex: abc ) to the body tag in header.php when a specific page template (ex: abc.php) loads.
First I used the code bellow in header.php it did not work.
<body <?php if ( is_page_template( 'template-parts/page/abc.php' ) ) { body_class( 'abc' ); } else { body_class(); } ?>>
Then I added bellow code to functions.php but it is not working either.
add_filter( 'body_class','abc_class' );
function abc_class( $classes ) {
if ( is_page_template( 'template-parts/page/abc.php' ) ) {
$classes[] = 'abc';
}
return $classes;
}
I do not understand what I am doing wrong here. How can I fix this?
First of all the is_page_template() function works for WP templates, I mean which have in the beginning of the file
/*
* Template name: Your Super Template
*/
So, the function returns true is a page with this template is actually displaying. I will tell you more, you even do not have to add custom body classes because function body_class() adds specific unique classes for each page template :)
Second, the page templates can be in the theme directory itself or in 1-level subdirectory, so
<body <?php if ( is_page_template( 'abc.php' ) ) { body_class( 'abc' ); } else { body_class(); } ?>>
or
<body <?php if ( is_page_template( 'page-templates/abc.php' ) ) { body_class( 'abc' ); } else { body_class(); } ?>>
Your template in 2-level sub-directory, so it is not actually a Page Template, it is a template part.
So, in two words, the function is_page_template() is for Page Templates but not for template parts
I was able to add the following in functions.php so that it takes the slug and inject that to the body class every time a specific page loads.
add_filter( 'body_class', function( $classes ) {
if ( is_page() ){
$slug = get_queried_object()->post_name;
return array_merge( $classes, array( $slug ) );
}
return $classes;
} );
I'd like to display advanced custom fields on my archive page only but the following code keeps giving me an error:
<?php
do_action( 'editorial_post_categories' );
if ( is_single() ) {
// do something or nothing
} else {
<?php the_field('acf_123'); ?>
}
?>
The advanced custom field I like to display is "acf_123"
Looks like you're nesting <?php tags. You've opened one at the top, so you don't need to open them again. Try:
<?php
do_action( 'editorial_post_categories' );
if ( is_single() ) {
// do something or nothing
} else {
the_field('acf_123');
}
?>
That is, remove the <?php and ?> around the_field() call.
I think my previous question was over complicated and, to be honest, was confusing me, nevermind the people trying to answer.
I currently have to pages, both with one category of posts assigned to them, however both post pages are using the same content.php and content-single.php, but i was both pages to use different iterations of these pages for cosmetic reasons.
As an example, visit http://dev.n8geeks.com/blog/ and click on the first blog post. It displays a thumbnail, which is cool and is what i want. However, now on the "videos" page as seen here; http://dev.n8geeks.com/videos/ (once there, click on the post) it also shows the thumbnail box (but no thumbnails will be attached on this posts page category).
This is why i need to user different iterations of content.php and content-single.php, but i simply don't know how. It would also be great if the "videos" page had the same formatting as the "blog" page, but again, i don't know how to achieve this.
The code i'm using for the current "videos" page is as below.
<?php get_header(); ?>
<div id="content">
<div id="main">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; else: endif; ?>
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1 class="entry-title"><a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a></h1>
<p><?php the_content(); ?>
<?php endwhile; else: endif; ?>
</div>
</div>
<?php get_footer(); ?>
Thanks in advance, i really appreciate any help like you wouldn't believe - it's 4:33am and i'm going insane trying to find a fix for this.
Regards
Still extremely confusing, haha, but from the sounds of it you want different templates to show up based on which category the post is in when you're viewing a single post?
If so, you could try setting this as you single.php:
<?php get_header(); ?>
<?php
if ( have_posts() ) { the_post(); rewind_posts(); }
if ( in_category(1) || in_category(2) ) {
include(TEMPLATEPATH . '/single-cat1-2.php');
}
else {
include(TEMPLATEPATH . '/single-default.php');
}
?>
<?php get_footer(); ?>
(from http://wordpress.org/support/topic/alternate-single-post-template-for-specific-categories)
And create the files 'single-cat1-2.php' and 'single-default.php', just add to the if statement checking to see if the post is in a certain category (or categories) and load the correct template. You can use ID, name, and their slug as selectors for the in_category function as well, read more here.
EDIT:
Kay, well you do need to learn plugin programming to really do this. I've begun a quick plugin for you to help you out. It works, just isn't perfect. You could definitely use a different system, like tying the categories in the category menu, but I didn't feel like playing with the Settings API for that page.
So make a new directory in your plugins directory, call it PostCatTheme, make a new file in there called index.php and put this in it:
<?php
/*
* Plugin Name: Post Category Templates
*/
//Replace __FILE__ with whatever the real path is because of symbolic link
/**
* Allows declarations of which categories a single-post template is assigned to
*/
class WordpressPostCatTheme
{
private $pluginDir, $templates;
function __construct ()
{
$this->pluginDir = dirname(__FILE__);
add_action("init", array($this, "load"));
add_filter('single_template', array($this, 'get_post_template'));
}
public function WPCT_deactivate ()
{
delete_option("PostCatTheme_templates");
}
public function load ()
{
register_deactivation_hook( __FILE__, array(&$this, 'WPCT_deactivate') );
$this->templates = get_option("PostCatTheme_templates");
if ($this->templates === FALSE)
{
$this->templates = $this->get_post_templates();
update_option("PostCatTheme_templates", $this->templates);
}
}
// This function scans the template files of the active theme,
// and returns an array of [category] => {file}.php]
public function get_post_templates()
{
$themes = get_themes();
$theme = get_current_theme();
$templates = $themes[$theme]['Template Files'];
$post_templates = array();
$base = array(trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()));
foreach ((array)$templates as $template)
{
$template = WP_CONTENT_DIR . str_replace(WP_CONTENT_DIR, '', $template);
$basename = str_replace($base, '', $template);
// don't allow template files in subdirectories
if (false !== strpos($basename, '/'))
continue;
$template_data = implode('', file( $template ));
$categories = '';
if (preg_match( '|Categories (.*)$|mi', $template_data, $categories))
$categories = _cleanup_header_comment($categories[1]);
//The categories are split by a | (pipe), if there aren't any pipes, assume it's just
//one category, otherwise split at the pipe
if (empty($categories))
continue;
if (strpos($categories, "|") === FALSE)
$categories = array($categories);
else
$categories = explode("|", $categories);
foreach ($categories as $category)
{
if (!empty($category))
{
if (isset($post_templates[$category]))
throw new Exception("Error, category assigned to more than one template");
if(basename($template) != basename(__FILE__))
$post_templates[trim($category)] = $basename;
}
}
}
//file_put_contents($this->pluginDir . "/log", json_encode($post_templates));
return $post_templates;
}
// Filter the single template value, and replace it with
// the template chosen by the user, if they chose one.
function get_post_template($template)
{
global $post;
$cats = wp_get_post_categories($post->ID);
//Go through each category, until one hits
foreach ($cats as $c)
{
$templateP = $this->templates[$c];
if(!empty($templateP) && file_exists(TEMPLATEPATH . "/{$templateP}"))
{
$template = TEMPLATEPATH . "/{$templateP}";
break;
}
}
return $template;
}
}
if (!isset($PostCatThemePlugin))
$PostCatThemePlugin = new WordpressPostCatTheme;
?>
After that, in your custom single.php template, add the code Categories: 1|2 in the header section (where Template Name is). Whenever you change or add these, make sure to deactivate and reactivate the plugin, to refresh the cache that this information is stored in.
To get the ID of a category, edit a category and in the URL, the number after tag_ID= is the category's ID.
Hope that helps some,
Max