I need to add a unique CSS style to the articles and single pages sidebar on my site to diferenciate from the home page. The sidebar already has a style called ".sidebar" and it works when I change it, but it affects all the site sidebars.
I've been looking at the sidebars.php file and see there are "if" lines for home or single pages, but don't know what to change. Keep in mind I'm not a programmer.
The code I think should be modified is (sidebar.php):
<aside class="sidebar">
<?php
wp_reset_query();
if ( is_home() ){
$sidebar_home = tie_get_option( 'sidebar_home' );
if( $sidebar_home )
dynamic_sidebar ( sanitize_title( $sidebar_home ) );
else dynamic_sidebar( 'primary-widget-area' );
}elseif( is_page() ){
global $get_meta;
$tie_sidebar_pos = $get_meta["tie_sidebar_pos"][0];
if( $tie_sidebar_pos != 'full' ){
$tie_sidebar_post = sanitize_title($get_meta["tie_sidebar_post"][0]);
$sidebar_page = tie_get_option( 'sidebar_page' );
if( $tie_sidebar_post )
dynamic_sidebar($tie_sidebar_post);
elseif( $sidebar_page )
dynamic_sidebar ( sanitize_title( $sidebar_page ) );
else dynamic_sidebar( 'primary-widget-area' );
}
}elseif ( is_single() ){
global $get_meta;
$tie_sidebar_pos = $get_meta["tie_sidebar_pos"][0];
if( $tie_sidebar_pos != 'full' ){
$tie_sidebar_post = sanitize_title($get_meta["tie_sidebar_post"][0]);
$sidebar_post = tie_get_option( 'sidebar_post' );
if( $tie_sidebar_post )
dynamic_sidebar($tie_sidebar_post);
elseif( $sidebar_post )
dynamic_sidebar ( sanitize_title( $sidebar_post ) );
else dynamic_sidebar( 'primary-widget-area' );
}
}elseif ( is_category() ){
$category_id = get_query_var('cat') ;
$cat_sidebar = tie_get_option( 'sidebar_cat_'.$category_id ) ;
$sidebar_archive = tie_get_option( 'sidebar_archive' );
if( $cat_sidebar )
dynamic_sidebar ( sanitize_title( $cat_sidebar ) );
elseif( $sidebar_archive )
dynamic_sidebar ( sanitize_title( $sidebar_archive ) );
else dynamic_sidebar( 'primary-widget-area' );
}else{
$sidebar_archive = tie_get_option( 'sidebar_archive' );
if( $sidebar_archive ){
dynamic_sidebar ( sanitize_title( $sidebar_archive ) );
}
else dynamic_sidebar( 'primary-widget-area' );
}
?>
</aside>
Thanks in advance for any help solving this.
your article should have a unique page or post id, have a look at the page/post source code to get the id. Your css will look something like #post_455 .sidebar {
/* your unique article styles */
}
ps - you shouldn't need to play around with sidebar.php
Related
I am using the following code to display widgets by permalinks...
<?php
global $post;
$permalink = get_permalink( $post->ID );
if ( strpos( $permalink, '/one/' ) !== false || strpos( $permalink, '/one-two/' ) !== false ) {
echo dynamic_sidebar( 'disclaimer_cop' );
} elseif ( strpos( $permalink, '/two/' ) !== false || strpos( $permalink, '/two-one/' ) !== false ) {
echo dynamic_sidebar( 'disclaimer' );
} ?>
This code is dynamically displaying the widgets on the correct pages but when viewing the page the widget position is being followed by a random "1" in the HTML, like this.
<div class="widget_text disclaimer"> <div class="textwidget custom-html-widget">*Disclaimer text.</div>
1
Any ideas as to what is wrong?
Remove the echo. Looks like you're outputting the result as well as the content.
<?php
global $post;
$permalink = get_permalink( $post->ID );
if ( strpos( $permalink, '/one/' ) !== false || strpos( $permalink, '/one-two/' ) !== false ) {
dynamic_sidebar( 'disclaimer_cop' );
} elseif ( strpos( $permalink, '/two/' ) !== false || strpos( $permalink, '/two-one/' ) !== false ) {
dynamic_sidebar( 'disclaimer' );
}
?>
I am not sure how you are implentening on your case the code you specified. If you do it via add_shortcode() and then you call your shortcode on a widget you will just need to change the echo with a return.
Hope that helps.
Cheers!
I am trying to get the intro text area to show on single posts, however, I can't quite figure out how to get it to work.
if( ! function_exists( 'ctsi_intro_text' ) ) :
add_action( 'genesis_before_loop', 'ctsi_intro_text', 5 );
function ctsi_intro_text() {
if( ( ! is_page() || is_front_page() ) && ! is_home() ) return;
if( ( is_page() && get_field( 'ctsi_intro_text' ) ) || ( is_home() && get_field( 'ctsi_intro_text', get_option( 'page_for_posts' ) ) ) ) :
?>
<div class="intro-text">
<div class="wrap">
<?php if( is_page() || is_single() ) : ?>
<?php echo get_field( 'ctsi_intro_text' ) ? get_field( 'ctsi_intro_text' ) : ''; ?>
<?php else : ?>
<?php echo get_field( 'ctsi_intro_text', get_option( 'page_for_posts' ) ) ? get_field( 'ctsi_intro_text', get_option( 'page_for_posts' ) ) : ''; ?>
<?php endif; ?>
</div>
Genesis is an excellent framework but certain parts of the documentation can be a little lacking. If you want to customise your single post template, you can do the following
Create a file in your child theme folder called single.php Make sure the last line in this file is the following:
enter code here
genesis();
Use the Genesis theme hooks and filters to manipulate your markup. The visual theme hook guide is an excellent resource for this. See: https://genesistutorials.com/visual-hook-guide/
So, if you wanted to modify the entry content, you could do something like this:
remove_action('genesis_entry_content', 'genesis_do_post_content');
add_action( 'genesis_entry_content', 'custom_entry_content' ); // Add custom loop
function custom_entry_content() {
//Custom Entry Content
}
Hope this help.
I'm using the Avada theme in wordpress. I'm trying to add some post custom fields into the scrollover (shown by mouse hover). I tried to edit the template file that controls the scrollover but without success. I got an error message invalid ">".
I tried to use this code to show the custom field:
<?php echo get_post_meta( $post_id,'the_price',); ?>
The Avada function template is as follows:
if ( ! function_exists( 'avada_render_rollover' ) ) {
function avada_render_rollover( $post_id, $post_permalink = '', $display_woo_price = false, $display_woo_buttons = false, $display_post_categories = 'default', $display_post_title = 'default', $gallery_id = '' ) {
// Retrieve the permalink if it is not set
if ( ! $post_permalink ) {
$post_permalink = get_permalink( $post_id );
}
// Check if theme options are used as base or if there is an override for post categories
if ( 'enable' == $display_post_categories ) {
$display_post_categories = true;
} elseif ( 'disable' == $display_post_categories ) {
$display_post_categories = false;
} else {
$display_post_categories = ! Avada()->settings->get( 'cats_image_rollover' );
}
// Check if theme options are used as base or if there is an override for post title
if ( 'enable' == $display_post_title ) {
$display_post_title = true;
} elseif ( 'disable' == $display_post_title ) {
$display_post_title = false;
} else {
$display_post_title = ! Avada()->settings->get( 'title_image_rollover' );
}
// Set the link on the link icon to a custom url if set in page options
$icon_permalink = ( fusion_get_page_option( 'link_icon_url', $post_id ) != null ) ? fusion_get_page_option( 'link_icon_url', $post_id ) : $post_permalink;
if ( '' == fusion_get_page_option( 'image_rollover_icons', $post_id ) || 'default' == fusion_get_page_option( 'image_rollover_icons', $post_id ) ) {
if( ! Avada()->settings->get( 'link_image_rollover' ) && ! Avada()->settings->get( 'zoom_image_rollover' ) ) { // link + zoom
$image_rollover_icons = 'linkzoom';
} elseif( ! Avada()->settings->get( 'link_image_rollover' ) && Avada()->settings->get( 'zoom_image_rollover' ) ) { // link
$image_rollover_icons = 'link';
} elseif( Avada()->settings->get( 'link_image_rollover' ) && ! Avada()->settings->get( 'zoom_image_rollover' ) ) { // zoom
$image_rollover_icons = 'zoom';
} elseif( Avada()->settings->get( 'link_image_rollover' ) && Avada()->settings->get( 'zoom_image_rollover' ) ) { // link
$image_rollover_icons = 'no';
} else {
$image_rollover_icons = 'linkzoom';
}
} else {
$image_rollover_icons = fusion_get_page_option( 'image_rollover_icons', $post_id );
}
// Set the link target to blank if the option is set
$link_target = ( 'yes' == fusion_get_page_option( 'link_icon_target', $post_id ) || 'yes' == fusion_get_page_option( 'post_links_target', $post_id ) ) ? ' target="_blank"' : '';
?>
<div class="fusion-rollover">
<div class="fusion-rollover-content">
<?php if ( 'no' != $image_rollover_icons && 'product' != get_post_type( $post_id ) ) : // Check if rollover icons should be displayed ?>
<?php if ( 'zoom' != $image_rollover_icons ) : // If set, render the rollover link icon ?>
<a class="fusion-rollover-link" href="<?php echo $icon_permalink; ?>"<?php echo $link_target; ?>>Permalink</a>
<?php endif; ?>
<?php if ( 'link' != $image_rollover_icons ) : // If set, render the rollover zoom icon ?>
<?php
// Get the image data
$full_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'full' );
if ( ! is_array( $full_image ) ) {
$full_image = array();
$full_image[0] = '';
}
// If a video url is set in the post options, use it inside the lightbox
if ( fusion_get_page_option( 'video_url', $post_id ) ) {
$full_image[0] = fusion_get_page_option( 'video_url', $post_id );
}
?>
<?php if ( 'linkzoom' == $image_rollover_icons || '' === $image_rollover_icons ) : // If both icons will be shown, add a separator ?>
<div class="fusion-rollover-sep"></div>
<?php endif; ?>
<?php if ( $full_image[0] ) : // Render the rollover zoom icon if we have an image ?>
<?php
// Only show images of the clicked post
if ( 'individual' == Avada()->settings->get( 'lightbox_behavior' ) ) {
$lightbox_content = avada_featured_images_lightbox( $post_id );
$data_rel = sprintf( 'iLightbox[gallery%s]', $post_id );
// Show the first image of every post on the archive page
} else {
$lightbox_content = '';
$data_rel = sprintf( 'iLightbox[gallery%s]', $gallery_id );
}
?>
<a class="fusion-rollover-gallery" href="<?php echo $full_image[0]; ?>" data-id="<?php echo $post_id; ?>" data-rel="<?php echo $data_rel; ?>" data-title="<?php echo get_post_field( 'post_title', get_post_thumbnail_id( $post_id ) ); ?>" data-caption="<?php echo get_post_field( 'post_excerpt', get_post_thumbnail_id( $post_id ) ); ?>">Gallery</a><?php echo $lightbox_content; ?>
<?php endif; ?>
<?php endif; ?>
<?php endif; ?>
<?php if ( $display_post_title ) : // Check if we should render the post title on the rollover ?>
<h4 class="fusion-rollover-title"><a href="<?php echo $icon_permalink; ?>"<?php echo $link_target; ?>><?php echo get_the_title( $post_id ); ?></a></h4>
<?php endif; ?>
<?php
// Check if we should render the post categories on the rollover
if ( $display_post_categories ) {
// Determine the correct taxonomy
if ( 'post' == get_post_type( $post_id ) ) {
$post_taxonomy = 'category';
} elseif ( 'avada_portfolio' == get_post_type( $post_id ) ) {
$post_taxonomy = 'portfolio_category';
} elseif ( 'product' == get_post_type( $post_id ) ) {
$post_taxonomy = 'product_cat';
}
echo get_the_term_list( $post_id, $post_taxonomy, '<div class="fusion-rollover-categories">', ', ', '</div>' );
}
?>
<?php if ( $display_woo_price ) : // Check if we should render the woo product price ?>
<?php woocommerce_get_template( 'loop/price.php' ); ?>
<?php endif; ?>
<?php if ( $display_woo_buttons ) : // Check if we should render the woo "add to cart" and "details" buttons ?>
<div class="fusion-product-buttons">
<?php woocommerce_get_template( 'loop/add-to-cart.php' ); ?>
<span class="fusion-rollover-linebreak"></span>
<a class="fusion-show-details-button" href="<?php echo post_permalink(); ?>"><?php _e( 'Details', 'Avada' ); ?></a>
</div>
<?php endif; ?>
</div>
</div>
<?php
}
}
Usually to use $post_id you need first to declare global $post
I need to shorten the title in the previous and next buttons in a WP theme.
Here's the code:
<?php
/* ----------------------------------------------------------------------------------
Display navigation to next/previous pages when applicable.
---------------------------------------------------------------------------------- */
if ( ! function_exists( 'thinkup_input_nav' ) ) :
function thinkup_input_nav( $nav_id ) {
global $wp_query, $post;
// Don't print empty markup on single pages if there's nowhere to navigate.
if ( is_single() ) {
$previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
$next = get_adjacent_post( false, '', false );
if ( ! $next && ! $previous )
return;
}
// Don't print empty markup in archives if there's only one page.
if ( $wp_query->max_num_pages < 2 && ( is_home() || is_archive() || is_search() ) )
return;
?>
<nav role="navigation" id="<?php echo $nav_id; ?>">
<?php if ( is_single() ) : ?>
<?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-icon"><i class="icon-angle-left icon-large"></i></span><span class="meta-nav">%title</span>' ); ?>
<?php next_post_link( '<div class="nav-next">%link</div>', '<span class="meta-nav">%title</span><span class="meta-icon"><i class="icon-angle-right icon-large"></i></span>' ); ?>
<?php elseif ( $wp_query->max_num_pages > 1 && ( is_home() || is_archive() || is_search() ) ) : // navigation links for home, archive, and search pages ?>
<?php if ( get_next_posts_link() ) : ?>
<div class="nav-previous"><?php next_posts_link( __( 'Older posts', 'lan-thinkupthemes') ); ?></div>
<?php endif; ?>
<?php if ( get_previous_posts_link() ) : ?>
<div class="nav-next"><?php previous_posts_link( __( 'Newer posts', 'lan-thinkupthemes') ); ?></div>
<?php endif; ?>
<?php endif; ?>
</nav><!-- #<?php echo $nav_id; ?> -->
<?php
}
endif;
/* ----------------------------------------------------------------------------------
Display navigation to next/previous image when applicable.
---------------------------------------------------------------------------------- */
if ( ! function_exists( 'thinkup_input_imagesnav' ) ) :
function thinkup_input_imagesnav() {
global $wp_query, $post;
?>
<nav role="navigation" id="nav-below">
<div class="nav-previous"><?php previous_image_link( 'false', '<div class="nav-previous"><span class="meta-icon"><i class="icon-angle-left icon-large"></i></span><span class="meta-nav">' . __( 'Previous', 'lan-thinkupthemes') . ' </span></div>' ); ?></div>
<div class="nav-next"><?php next_image_link( 'false', '<span class="meta-nav">' . __( 'Next', 'lan-thinkupthemes') . '</span><span class="meta-icon"><i class="icon-angle-right icon-large"></i></span>' ); ?></div>
</nav><!-- #image-navigation -->
<?php
}
endif;
/* Display comments at bottom of post, page and project pages. */
function thinkup_input_allowcomments() {
if ( comments_open() || '0' != get_comments_number() )
comments_template( '/comments.php', true );
}
/* ----------------------------------------------------------------------------------
Returns true if a blog has more than 1 category.
---------------------------------------------------------------------------------- */
function thinkup_input_categorizedblog() {
if ( false === ( $all_the_cool_cats = get_transient( 'all_the_cool_cats' ) ) ) {
// Create an array of all the categories that are attached to posts
$all_the_cool_cats = get_categories( array(
'hide_empty' => 1,
) );
// Count the number of categories that are attached to the posts
$all_the_cool_cats = count( $all_the_cool_cats );
set_transient( 'all_the_cool_cats', $all_the_cool_cats );
}
if ( '1' != $all_the_cool_cats ) {
return true;
} else {
return false;
}
}
/* Flush out the transients used in thinkup_input_categorizedblog. */
function thinkup_input_transient_flusher() {
delete_transient( 'all_the_cool_cats' );
}
add_action( 'edit_category', 'thinkup_input_transient_flusher' );
add_action( 'save_post', 'thinkup_input_transient_flusher' );
What I tried was to add substring to this line:
<?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-icon"><i class="icon-angle-left icon-large"></i></span><span class="meta-nav">%title</span>' ); ?>
to:
<?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-icon"><i class="icon-angle-left icon-large"></i></span><span class="meta-nav"><?php echo substr(%title,0,10)?></span>' ); ?>
but upon reloading the page it displayed an empty text instead.
What am I doing wrong?
You can override the previous_post_link() function in your functions.php file of your theme as like this :
function previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '',$limit = -1) {
if ( is_attachment() )
$post = & get_post($GLOBALS['post']->post_parent);
else
$post = get_previous_post($in_same_cat, $excluded_categories);
if ( !$post )
return;
$title = apply_filters('the_title', $post->post_title, $post);
if ($limit>-1) {$title = substr($title,0,$limit).'…';}
$string = '<a href="'.get_permalink($post->ID).'">';
$link = str_replace('%title', $title, $link);
$link = $pre . $string . $link . '</a>';
$format = str_replace('%link', $link, $format);
echo $format;
And then you can call the function with the additional argument to define your desired trim length:
<li><?php previous_post_link('%link','« %title',FALSE,'',20); ?></li>
Hope it helps you.
Taken from https://wordpress.org/support/topic/using-substr-to-limit-characters-in-post-title
So, I added an input field to one of my post types.
What I'm trying to do is put a link (google.com, facebook.com, any link) into this field, and have it output on the front end into a button.
Below is my button. I would like it to go to the link I placed on the input field when I click the button.
<?php echo __('Purchase Now','Vierra'); ?>
Here's the code for the metabox:
<?php
add_action( 'add_meta_boxes', 'cart_meta_box_add' );
function cart_meta_box_add() {
add_meta_box( 'cart-meta-box-id', 'Put cart Here!', 'cart_meta_box_cb', 'Room', 'side', 'high' );
}
function cart_meta_box_cb() {
wp_nonce_field( basename( __FILE__ ), 'cart_meta_box_nonce' );
$value = get_post_meta(get_the_ID(), 'cart_key', true);
$html = '<label>cart: </label><input type="text" name="cart" value="'.$value.'"/>';
echo $html;
}
add_action( 'save_post', 'cart_meta_box_save' );
function cart_meta_box_save( $post_id ){
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( !isset( $_POST['cart_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['cart_meta_box_nonce'], basename( __FILE__ ) ) ) return;
if( !current_user_can( 'edit_post' ) ) return;
if( isset( $_POST['cart'] ) )
update_post_meta( $post_id, 'cart_key', esc_attr( $_POST['cart'], $allowed ) );
}
?>
Thank you for the help!
It's the same function used in the admin: get_post_meta(). Assuming the button is being printed inside the loop *:
<?php
if( $field = get_post_meta( get_the_ID(), 'cart_key', true ) ) {
?>
<a href="<?php echo $field; ?>" id="btn-book-now" class="btn-custom">
<?php echo __('Purchase Now','Vierra'); ?>
</a>
<?php
}
?>
* Otherwise, you'd use $post->ID instead of get_the_ID().