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.
Related
Hi :) I am working on my site with the "flexible post widget" to show the related contents or the content that I am interested in showing on the sidebar.
The widget works very well and I'm interested in continuing to use it, especially for the design and adaptability options with my site.
The problem? It does not include a function for "exclude the current post".
I was reviewing the code (I add it to this thread) and I think I could add some lines of code for this function (exclude the post I'm reading).
Unfortunately I'm new to programming and I'm lost with what and where to add it.
Could you help me?
<?php
/**
* Flexible Posts Widget: Default widget template
*
* #since 3.4.0
*
* This template was added to overcome some often-requested changes
* to the old default template (widget.php).
*/
// Block direct requests
if ( !defined('ABSPATH') )
die('-1');
echo $before_widget;
if ( ! empty( $title ) )
echo $before_title . $title . $after_title;
if ( $flexible_posts->have_posts() ):
function be_exclude_current_post( $args ) {
if( is_singular() && !isset( $args['post__in'] ) )
$args['post__not_in'] = array( get_the_ID() );
return $args;
}
add_filter( 'widget_posts_args', 'be_exclude_current_post' );
?>
<ul class="dpe-flexible-posts">
<?php while ( $flexible_posts->have_posts() ) : $flexible_posts->the_post(); global $post; ?>
<li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<a href="<?php echo the_permalink(); ?>">
<?php
if ( $thumbnail == true ) {
// If the post has a feature image, show it
if ( has_post_thumbnail() ) {
the_post_thumbnail( $thumbsize );
// Else if the post has a mime type that starts with "image/" then show the image directly.
} elseif ( 'image/' == substr( $post->post_mime_type, 0, 6 ) ) {
echo wp_get_attachment_image( $post->ID, $thumbsize );
}
}
?>
<div class="title"><?php the_title(); ?></div>
</a>
</li>
<hr>
<?php endwhile; ?>
</ul><!-- .dpe-flexible-posts -->
<?php
endif; // End have_posts()
echo $after_widget;
Add this to your theme's 'functions.php' and remove it from the widget template.
function be_exclude_current_post( $args ) {
if( is_singular() && !isset( $args['post__in'] ) )
$args['post__not_in'] = array( get_the_ID() );
return $args;
}
add_filter( 'dpe_fpw_args', 'be_exclude_current_post' );
I have customised my Wordpress site design to use the featured image for posts quite excessively. This is why I need to require all post made by non-admins to require a set featured image.
How is this possible?
You need to hook the Publish Action in a custom PlugIn you write. Although this is to require a title, this should get you started, you just need to check if a featured image was assigned.
add_action( 'pre_post_update', 'bawdp_dont_publish' );
function bawdp_dont_publish()
{
global $post;
if ( strlen( $post->title ) < 10 ) {
wp_die( 'The title of your post have to be 10 or more !' );
}
}
Look at (has_post_thumbnail( $post->ID )) to determine if a post has a featured image.
Given Gary's example above, I've written the following to my functions.php file:
function featured_image_requirement() {
if(!has_post_thumbnail()) {
wp_die( 'You forgot to set the featured image. Click the back button on your browser and set it.' );
}
}
add_action( 'pre_post_update', 'featured_image_requirement' );
I'd much rather see this in a plugin as well - there's one called Mandatory Field but it doesn't work with scheduled posts. Both are not really eloquent solutions.
you can use a plugin
https://wordpress.org/plugins/require-featured-image/
or you can copy and paste below code in your wordpress theme functions.php file:
<?php
/**
* Require a featured image to be set before a post can be published.
*/
add_filter( 'wp_insert_post_data', function ( $data, $postarr ) {
$post_id = $postarr['ID'];
$post_status = $data['post_status'];
$original_post_status = $postarr['original_post_status'];
if ( $post_id && 'publish' === $post_status && 'publish' !== $original_post_status ) {
$post_type = get_post_type( $post_id );
if ( post_type_supports( $post_type, 'thumbnail' ) && ! has_post_thumbnail( $post_id ) ) {
$data['post_status'] = 'draft';
}
}
return $data;
}, 10, 2 );
add_action( 'admin_notices', function () {
$post = get_post();
if ( 'publish' !== get_post_status( $post->ID ) && ! has_post_thumbnail( $post->ID ) ) { ?>
<div id="message" class="error">
<p>
<strong><?php _e( 'Please set a Featured Image. This post cannot be published without one.' ); ?></strong>
</p>
</div>
<?php
}
} );
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
So, I'm developing a site for a client where the homepage is built like a one-page scroller, but I also need the functionality of additional pages outside of the single homepage. I've built a custom post type for these sections and used this code to display them on the homepage.
<?php query_posts( array('post_type'=>'homepage', 'posts_per_page' => 1000, 'orderby' => 'menu_order', 'order' => 'ASC') ); ?>
<?php if(have_posts()): while(have_posts()): the_post(); ?>
<?php
global $post;
$slug = $post->post_name;
locate_template(
array(
"template-$slug.php",
'template-main.php'
), true
);
?>
<?php endwhile; endif; ?>
So, as you can see, this is automatically pulling content and displaying it using page templates based on the post slug, however, I need to allow my client to display the content based on a page template chosen in a dropdown and I've used this code to create a dropdown UI that displays the page templates.
add_action( 'add_meta_boxes', 'add_custom_page_attributes_meta_box' );
function add_custom_page_attributes_meta_box(){
global $post;
if ( 'page' != $post->post_type && post_type_supports($post->post_type, 'page-attributes') ) {
add_meta_box( 'custompageparentdiv', __('Template'), 'custom_page_attributes_meta_box', NULL, 'side', 'core');
}
}
function custom_page_attributes_meta_box($post) {
$template = get_post_meta( $post->ID, '_wp_page_template', 1 ); ?>
<select name="page_template" id="page_template">
<?php $default_title = apply_filters( 'default_page_template_title', __( 'Default Template' ), 'meta-box' ); ?>
<option value="default"><?php echo esc_html( $default_title ); ?></option>
<?php page_template_dropdown($template); ?>
</select><?php
}
add_action( 'save_post', 'save_custom_page_attributes_meta_box' );
function save_custom_page_attributes_meta_box( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) return;
if ( ! current_user_can( 'edit_post', $post_id ) ) return;
if ( ! empty( $_POST['page_template'] ) && get_post_type( $post_id ) != 'page' ) {
update_post_meta( $post_id, '_wp_page_template', $_POST['page_template'] );
}
}
So, the problem I'm facing now is how to display all the custom posts in my main page according to the chosen page template.
Thanks so much!
J
Wordpress actually only uses _wp_page_template meta field for page type posts. If you want to change the template, you can use the filter single template. One thing I would recommend is you place good notes that you are using this in your theme/ plugin....
btw update cpt to your post-type
function load_cpt_template($single_template) {
global $post;
if ($post->post_type == 'cpt') {
$new_template = get_post_meta( $post->ID, '_wp_page_template', true );
// if a blank field or not valid do nothing, load default..
if( is_file($new_template) )
$single_template = $new_template;
}
return $single_template;
}
add_filter( 'single_template', 'load_cpt_template' );
I am using a WooCommerce plugin for WordPress. Now in a file page.php I am trying to get the current opened page id via $post->ID and the_id() but all I am getting is the product ID and not the current page ID. Probably because WooCommerce overwrites the loop ?
What can I do ?
It might be too late, but i was trying to get the same result as you and this below worked pretty well for me:
<?php
if(is_shop()){
$page_id = woocommerce_get_page_id('shop');
}else{
$page_id = $post->ID;
}
?>
Hope this will help you.
if you are trying to get the particular page id. Try with this.
if ( wc_get_page_id( 'name of page' ) != get_the_ID()):
This worked fine for me
global $post;$post->ID; or global $wp_query;$wp_query->post->ID; don't work with woocommerce. You can get all the id's of woocommerce page here
You can try this, inspired by woocommerce_product_archive_description
add_action('woocommerce_after_main_content', 'custom_name', 11, 2);
function custom_name() {
if ( is_post_type_archive( 'product' ) && 0 === absint( get_query_var( 'paged' ) ) ) :
global $post;
$shop_page = get_post( wc_get_page_id( 'shop' ) );
if ( $shop_page ) {
$post = $shop_page;
}
// your code here
endif;
}
$woo_pages_id = array(
//get_option( 'woocommerce_shop_page_id' ),
get_option( 'woocommerce_cart_page_id' ),
get_option( 'woocommerce_checkout_page_id' ),
//get_option( 'woocommerce_pay_page_id' ),
//get_option( 'woocommerce_thanks_page_id' ),
get_option( 'woocommerce_myaccount_page_id' ),
//get_option( 'woocommerce_edit_address_page_id' ),
//get_option( 'woocommerce_view_order_page_id' ),
get_option( 'woocommerce_terms_page_id' )
);
$current_page_id = get_the_ID();
if ( in_array($current_page_id, $woo_pages_id) ) :
get_template_part('content/woo', 'pages');
else :
get_template_part('content', 'page');
endif;
I'm using this for my page.php file.
put this line above your code or at starting of script.
global $post;
now you can use $post->ID;
thanks
Had the same problem found a solution here
The code I used on my shop page was:
<?php echo woocommerce_get_page_id('shop'); ?>
Basically replace the shop value with whatever woocommerce page you are trying to get.
More documentation on the function and some other helpful functions can be found here