Is there a way to customise a wordpress blog post so that it changes slightly based on the User?
eg. when showing a post around Salary and Bonuses on the company blog, I'd like to customise some of the text based on the employee level (director, Executive etc) - for example:
a paragraph of text about pension contribution should only appear to
directors reading the page
the bonus amount should change based on employee level.
I don't want to create several pages per category (eg. one page for the directors, one for the execs etc) - I would like to have just one blog post with some variable fields?
The codex has a great function outlined, current_user_can
http://codex.wordpress.org/Function_Reference/current_user_can
As well as WP_User_Query
http://codex.wordpress.org/Class_Reference/WP_User_Query
You can use both of these to accomplish your goal.
I needed to do something similar, I came up with custom roles based on the plugin "Capability Manager Enhanced".
In a file you can check the roles like:
<? if ( is_user_logged_in() ) : ?>
<?
global $current_user;
get_currentuserinfo();
$roles = $current_user->roles; //$roles is an array
if ( is_user_logged_in() && in_array("custom_role", $roles) ) {
echo('WORK HARDER!');
} else {
echo('NO CAKE FOR YOU!');
}
?>
<?php endif; ?>
Related
I am building a custom theme for WordPress. One thing I want to do is allow myself to switch the header from the edit page.
I know I can hard code in the header to switch based on the page ID or name, like this:
<?php
if(is_page(10)) {
get_header('new');
}
else {
get_header();
}
wp_head();
?>
But I want a drop down menu similar to the Page Template option in the sidebar. (See screenshot)
Screenshot of sidebar menu
I have looked for any online tutorials that cover this type of option, but they all cover the basic ID or name setup shown above.
Does anyone know of a good tutorial to create a drop down similar to Page Templates to use for multiple headers?
You can use Custom Metaboxes. That link contains a comprehensive tutorial on creating custom metaboxes for post meta fields by hand. You can also use a plugin like Advanced Custom Fields to create them.
Doing this would allow you to check for the header style value through get_post_meta() or get_field(), respectively.
<?php
// If using the Custom Metabox/post_meta approach:
$header_style = get_post_meta( get_the_ID(), 'my_custom_header', true );
// If using ACF:
$header_style = get_field( 'my_custom_header', get_the_ID() );
if( $header_style == 'new' ){
get_header('new');
if( $header_style == 'something-else' ){
get_header('something-else');
} else {
get_header();
}
?>
I'm using wordpress and my page has the URL http://proservicescontractors.com/services/
But when I go to the page in my dashboard with the above URL, any change I make does not show on the front end. I tried simply duplicating my content and that change did not show on the front end.
Not sure what to do, this has me completely baffled.
Any ideas?
Since they're custom post types, by default, they're not actually loaded into a page per se. You should read up on WordPress's template hierarchy. To give you a rough idea of what's happening:
WP looks at your URL, and since it recognises it as a custom post type archive, it will look for a template to use...
It will first look for archive-$post_type.php, or in your case, archive-services.php
If it can't find that, it will look for archive.php
If it can't find that, it will use index.php
The important thing to note is that archive pages don't actually show up in the admin area, since they simply gather up and display custom posts, so there's nothing for you to edit.
Now, if you really want to edit some content on the Services archive, you have two options:
Edit archive-services.php in a text editor.
This is the quick and dirty option; the downside is that it defies the point of a CMS.
Create a page template with it's own loop
Create a new page template called page-services.php and insert a loop in there to display your custom posts. To get you started:
<?php get_header(); ?>
<?php // The main loop
if (have_posts()) {
while (have_posts()) {
the_post();
}
} else {
echo 'No posts';
}
?>
<?php // Now for the services loop
// WP_Query arguments
// For additional options, see: https://codex.wordpress.org/Class_Reference/WP_Query#Parameters
$args = array (
'post_type' => array( 'services' ),
);
// The Query itself
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// Do something with the post
// In your case look at archive-services.php and see what
// that template does inside the loop
}
} else {
// no posts found
}
// Restore original Post Data
// Don't forget this, it's important
wp_reset_postdata();
?>
<?php get_footer();?>
You should then be able to apply that page template to your Services page; it should then display your posts below the page content. One thing to look out for is that WordPress will continue to load archive-services.php whenever you go to http://proservicescontractors.com/services/. While there are ways around this, the easiest fix would be to simply give your new page a different url, such as http://proservicescontractors.com/all-services/
Thanks for your help. I'm using yoast and I wanted to change the title and description. When you pointed out that it was a custom post type archive and not a page, I went back through yoast and found where I could change them under "Titles and Metas" > "Custom Post Type Archives" > "Services"
I have an author.php page that must look differently according to post author's role. If it's role is Editor or Admin, the bios is shown. Otherwise (for Author, for example), it is not. But I can't find it anywhere. I had a clue to use get_userdata, but couldn't manage to do so.
Thanks for the help!
You can check the $wp_query global to get the author's role:
global $wp_query;
$roles = $wp_query->queried_object->roles;
if( $roles[0] == 'editor' ) {
// do stuff
}
I'm trying to develop two different templates; one layout for a category listing, and a separate layout for subcategories.
I believe it'd require a conditional statement detecting if the current category is a category or subcategory.
I'm familiar with:
<?php if (is_category('Category A')) : ?>
<p>This is the text to describe category A</p>
<?php elseif (is_category('Category B')) : ?>
<p>This is the text to describe category B</p>
<?php else : ?>
<p>This is some generic text to describe all other category pages,
I could be left blank</p>
<?php endif; ?>
But I need this to be more dynamic and handle any category or subcategory without hard coding the cat ID number.
Any help would be appreciated, thank you in advance.
You can use get_category() (WordPress Codex) to get all information about a category. Using this, you could create a custom conditional function in your functions.php like this:
function is_subcategory ($catid) {
$currentcat = get_category($catid);
if ( $currentcat->parent ) {
return true;
} else {
return false;
}
}
Now you could use this function, to determine if a category has a parent category, and therefore is a subcategory.
Unfortunately I don't know the context of your posted code. You'll somehow need to get the category id. Do you use the loop the walk the categories or a custom sql-query?
If the answer is not helping, maybe you could add some surrounding code to your question.
How to count number of visitors for post under specific category ? Can I make such plugin who can do the whole magic ? I don't want plugin users to modify theme files or add code snippets in other theme files ...
something along the lines of adding to the post meta could do what you're wanting.
<?php
add_action('the_content', 'myplugincb');
function myplugincb() {
global $wp_query;
if (count($wp_query->posts) == 1) { //just do this for individual posts/pages
$pid = $wp_query->posts[0]->ID;
$key = 'myplugin_post_visit_counter';
update_post_meta($pid, $key, get_post_meta($pid, $key)+1);
}
}
function myplugin_show_viewed($post_id) {
return get_post_meta($post_id, 'myplugin_post_visit_counter');
}
You'd have to change that quite a few different ways depending on your desired result. You probably want to use something like Google Analytics if you're wanting to see specifics on pages visited and where the user came from etc.